Skip to content

[Circuit renderer] Compact rendering of classically controlled gate - #3665

Open
Dima Fedoriaka (fedimser) wants to merge 6 commits into
mainfrom
fedimser/clas-control-3
Open

[Circuit renderer] Compact rendering of classically controlled gate#3665
Dima Fedoriaka (fedimser) wants to merge 6 commits into
mainfrom
fedimser/clas-control-3

Conversation

@fedimser

@fedimser Dima Fedoriaka (fedimser) commented Aug 28, 2026

Copy link
Copy Markdown
Contributor
  • Object model changes (circuit.rs/circuit.ts):
    • Added ControlRegister to represent a register (classical or quantum) that is used to as control to a gate.
    • ControlRegister is Register plus boolean inverted.
    • Unitary.controls now is a vector of ControlRegister and can contain both classical and quantum registers.
    • ControlRegister extends Register in typescript rathe than wrapping it - this is done for backwards compatibility. Even though we changed type of controls, JSON looks exactly the same and old .qsc files will be correctly parsed.
    • To support this "flattened" serialization, I had to implement custom JSON serializer for ControlRegister.
    • Currenlty only classical controls can be inverted, but this model supports writing JSON representing quantum antic-control, and I checked that it would be rendered correctly.
  • Q#-to-circuit changes:
    • If there is a then/else branch with condition "resullt==One" or "result==Zero" and all gates inside have single target, instead of generating a group, emit gates without grouping them, but with added classical control.
    • Note that we allow quantum controls on these gates, so you can have gate in circuit with classical and quantum control (see Example 2 below).
  • Rendering changes:
    • Render controls of classically-controlled Unitary the same way as we would render quantum controls.
    • Render inverted controls with a hollow control dot (done with CSS).
  • Added circuit-to-Q# conversion for classically controlled gate (and a test).
  • Circuit-to-string rendering for classically controlled gate works without any changes, but updated Python tests to demonstrate new it.

Example 1 - teleportation

Before:
image

After:
image

Example 2

operation Tmp1() : Unit {
    use q = Qubit[4];
    H(q[0]);
    let m0 = M(q[0]);
    if (m0 == One) {
        X(q[1]);
        X(q[0]);
    } else {
        X(q[2]);
        Z(q[2]);
        CZ(q[3], q[2]);
        CNOT(q[2], q[3]);
    }
    let m1 = M(q[1]);
    if (m0 == Zero) {
        Rx(0.5, q[3]);
    } else {
        Ry(0.5, q[3]);
        if (m1==Zero) {
            X(q[1]);
        }
    }
}
image

Example 3 - negative cases (complex condition, 2-qubit gate).

operation Tmp2() : Unit {
    use q = Qubit[4];
    
    H(q[0]);
    H(q[1]);
    let m0 = M(q[0]);
    let m1 = M(q[1]);

    if (m0 == One and m1 == One) {
        X(q[1]);
    }
    if (m0 == Zero) {
        Rxx(0.5, q[2], q[3]);
    }
}
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant