Closed p51lee closed 11 months ago
Hello, I'm working with the Staq optimizer and it erroneously removes necessary if conditions.
if
To reproduce this issue, optimize the following bug.qasm:
bug.qasm
❯ cat bug.qasm OPENQASM 2.0; include "qelib1.inc"; gate cx_o0 q0,q1 { x q0; cx q0,q1; x q0; } qreg q[2]; creg c[1]; cx_o0 q[1],q[0]; swap q[0],q[1]; if(c==1) cx q[1],q[0]; measure q[0] -> c[0];
Since there are no prior measurements to change the initial state of c, the condition c==1 should be false and the cx gate should not be executed.
c==1
cx
❯ staq --version staq version 3.3 (c) 2019 - 2023 softwareQ Inc. All rights reserved. ❯ staq -O3 bug.qasm OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; creg c[1]; x q[1]; cx q[1],q[0]; x q[1]; swap q[0],q[1]; cx q[1],q[0]; measure q[0] -> c[0];
However, Staq optimizer removes the condition if (c==1). It should not be removed because:
if (c==1)
cx_o0 q[1],q[0];
q[0]
cx_o0
control qubit == 0
swap q[0],q[1];
q[1]
cx q[1],q[0];
I tracked this down, it was a bug with one of the visitor classes from qasmtools. Fixed now. Thanks for the bug report!
Hello, I'm working with the Staq optimizer and it erroneously removes necessary
if
conditions.To reproduce this issue, optimize the following
bug.qasm
:Since there are no prior measurements to change the initial state of c, the condition
c==1
should be false and thecx
gate should not be executed.However, Staq optimizer removes the condition
if (c==1)
. It should not be removed because:cx_o0 q[1],q[0];
flipsq[0]
to 1, becausecx_o0
is a controlled-x gate with conditioncontrol qubit == 0
.swap q[0],q[1];
nowq[1]
is 1.if (c==1)
,cx q[1],q[0];
will erroneously flipq[0]
.