Problem Statement
CtrlOp::getUnitaryMatrix() currently embeds the inner gate unitary via setBottomRightCorner, which implements I_{2^c} ⊗ U. That only corresponds to the intended controlled operator when control qubits sit on more significant register positions than the targets (e.g. cx(1, 0) in little-endian indexing).
For the common case cx(0, 1) — control on qubit 0, target on qubit 1 — the returned matrix does not match QuantumComputation with the same register indices. Operand order in the ctrl op is not reflected correctly; cx(0, 1) and cx(1, 0) are not distinguished as they should be.
This limits reliable use of getUnitaryMatrix() for controlled operations in optimizations, verification, and cross-checks against QuantumComputation or Qiskit. An existing CX test inadvertently compared cx(q[0], q[1]) against comp.cx(1, 0) rather than comp.cx(0, 1), hiding the mismatch.
Example:
// QCO: control q[0], target q[1]
ctrl(%c0, %t1) { cx ... }
// Should match qc::QuantumComputation::cx(0, 1), not cx(1, 0)
Proposed Solution
Introduce register-aware controlled embedding for CtrlOp::getUnitaryMatrix().
Problem Statement
CtrlOp::getUnitaryMatrix()currently embeds the inner gate unitary viasetBottomRightCorner, which implementsI_{2^c} ⊗ U. That only corresponds to the intended controlled operator when control qubits sit on more significant register positions than the targets (e.g.cx(1, 0)in little-endian indexing).For the common case
cx(0, 1)— control on qubit 0, target on qubit 1 — the returned matrix does not matchQuantumComputationwith the same register indices. Operand order in thectrlop is not reflected correctly;cx(0, 1)andcx(1, 0)are not distinguished as they should be.This limits reliable use of
getUnitaryMatrix()for controlled operations in optimizations, verification, and cross-checks againstQuantumComputationor Qiskit. An existing CX test inadvertently comparedcx(q[0], q[1])againstcomp.cx(1, 0)rather thancomp.cx(0, 1), hiding the mismatch.Example:
Proposed Solution
Introduce register-aware controlled embedding for
CtrlOp::getUnitaryMatrix().