Skip to content

Migrate control-lowering pass to QRef #3192

Open

Description

@hsajjad14

Design

For this example:

def controlled_gate_circuit(angle):
   qp.PauliX(0) #Flip to |1> for RX to act on the second wire
   qp.ctrl(qp.RX, control = (0), control_values=(1))(angle, wires=[1])
   return qp.state()

Current Flow

The mlir generated is:

      qref.custom "PauliX"() %1 : !qref.bit
      %2 = qref.get %0[ 0] : !qref.reg<2> -> !qref.bit
      qref.ctrl(%2) ctrlvals(%true) {
        %5 = qref.get %0[ 1] : !qref.reg<2> -> !qref.bit
        %extracted = tensor.extract %arg0[] : tensor<f64>
        qref.custom "RX"(%extracted) %5 : !qref.bit
      }

Instead of this going through the convert-to-value-semantics turning into:

      %out_ctrl_qubits, %results = quantum.ctrl(%3) ctrlvals(%true) (%4) : !quantum.bit -> !quantum.bit {
      ^bb0(%arg1: !quantum.bit):
        %extracted = tensor.extract %arg0[] : tensor<f64>
        %out_qubits_0 = quantum.custom "RX"(%extracted) %arg1 : !quantum.bit
        quantum.yield %out_qubits_0 : !quantum.bit
      }

which gets folded into this in the ctrl-lowering pass

%out_qubits_0, %out_ctrl_qubits = quantum.custom "RX"(%extracted) %2 ctrls(%out_qubits) ctrlvals(%true) : !quantum.bit ctrls !quantum.bit

We want to move this process earlier in the pipeline in reference semantics as it is easier and more intuitive there.

New Flow

Now we want to go directly from the original mlir in reference semantics:

      qref.custom "PauliX"() %1 : !qref.bit
      %2 = qref.get %0[ 0] : !qref.reg<2> -> !qref.bit
      qref.ctrl(%2) ctrlvals(%true) {
        %5 = qref.get %0[ 1] : !qref.reg<2> -> !qref.bit
        %extracted = tensor.extract %arg0[] : tensor<f64>
        qref.custom "RX"(%extracted) %5 : !qref.bit
      }

Then after the ctrl-loweringpass, it gets folded into this.

qref.custom "RX"(%extracted) %5 ctrls(%2) ctrlvals(%true) : !qref.bit

Where afterwards it get converted into value semantics:

%out_qubits_0, %out_ctrl_qubits = quantum.custom "RX"(%extracted) %q1 ctrls(%q0) ctrlvals(%true) : !quantum.bit ctrls !quantum.bit

The test for this behaviour is in TestFlatCircuits.mlir:

    // CHECK: [[ROT:%.+]], [[ROTctrl:%.+]] = quantum.custom "some_gate"(%arg0, %arg1) [[RX]] adj ctrls([[CNOT]]#1) ctrlvals(%arg2) : !quantum.bit ctrls !quantum.bit
    qref.custom "some_gate"(%arg0, %arg1) %q0 adj ctrls (%q1) ctrlvals (%arg2) : !qref.bit ctrls !qref.bit

The idea is to call --convert-to-reference-semantics on entering the control-lowering pass, do the lowering described above, then call --convert-to-value-semantics on exit.
Then for any pass that breaks, call conver-to-value-semantics at the beginning to fix it.

Activity

  1. changed the title [-]MIgrate `control-lowering` pass to QRef[/-] [+]Migrate `control-lowering` pass to QRef[/+] on Sep 8, 2026
  2. self-assigned this
    on Sep 21, 2026
  3. hsajjad14 commented on Sep 21, 2026

    @hsajjad14
    ContributorAuthor

    Instead of moving it earlier in the pipeline, I called convert-to-reference-semantics pass dynamically before this pass runs, then call convert-to-value-semantics before the beginning of the next pass (AdjointLowering pass).

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions