BQSKit / bqskit

Berkeley Quantum Synthesis Toolkit
Other
106 stars 31 forks source link

Compilation seems to be stuck with multiple cry gates #217

Closed jagandecapri closed 4 months ago

jagandecapri commented 5 months ago

Hi,

I'm trying to compile a circuit and the compilation was running indefinitely. I think I managed to narrow down the problem. For multiple cry gates in a QASM 2.0 definition saved in a file called random.qasm as following:

OPENQASM 2.0;
include "qelib1.inc";
qreg q[5];
creg meas[5];
cry(0) q[0],q[2];
cry(0) q[1],q[2];

The compilation using the following code below seems to run indefinitely:

from bqskit.ir import Circuit
from bqskit import compile

qc_bqskit = Circuit.from_file('random.qasm')

opt_level = 1

basic_compilation = compile(qc_bqskit, optimization_level=opt_level, seed=10)

Commenting out one of the cry seems to enable the compile function to successfully finish executing. For context, I'm trying to compile a larger circuit which has mutiple cry gates as following (pricingcall.qasm) for a specific machine model and I encountered the problem of compilation which doesn't seem to finish executing.

OPENQASM 2.0;
include "qelib1.inc";
qreg q[5];
creg meas[5];
ry(1.7376841305223034) q[0];
ry(1.4598019857964233) q[1];
cx q[1],q[0];
ry(1.3166149125118367) q[0];
cx q[1],q[0];
ry(3*pi/8) q[2];

// Problematic part
cry(0) q[0],q[2];
cry(0) q[1],q[2]; 
// ----------------

x q[1];
x q[3];
x q[4];
ccx q[1],q[4],q[3];
x q[1];
cx q[3],q[2];
u(0.2942523647695425,0,0) q[2];
cx q[3],q[2];
u3(0.2942523647695425,-pi,-pi) q[2];
cx q[3],q[2];
u(-0.1144919077447111,0,0) q[2];
cx q[3],q[2];
u(0.1144919077447111,0,0) q[2];
ccx q[3],q[0],q[2];
cx q[3],q[2];
u(0.1144919077447111,0,0) q[2];
cx q[3],q[2];
u(-0.1144919077447111,0,0) q[2];
ccx q[3],q[0],q[2];
cx q[3],q[2];
u(-0.2289838154894222,0,0) q[2];
cx q[3],q[2];
u(0.2289838154894222,0,0) q[2];
ccx q[3],q[1],q[2];
cx q[3],q[2];
u(0.2289838154894222,0,0) q[2];
cx q[3],q[2];
u(-0.2289838154894222,0,0) q[2];
ccx q[3],q[1],q[2];
x q[1];
ccx q[1],q[4],q[3];
x q[1];
x q[3];
x q[4];
barrier q[0],q[1],q[2],q[3],q[4];
measure q[0] -> meas[0];
measure q[1] -> meas[1];
measure q[2] -> meas[2];
measure q[3] -> meas[3];
measure q[4] -> meas[4];

Code

from bqskit.ir import Circuit
from bqskit.ir.gates import RZGate, SXGate, XGate, CNOTGate
from bqskit.compiler import MachineModel
from bqskit import compile

qc_bqskit = Circuit.from_file('pricingcall.qasm')

basis_gates = [RZGate(), SXGate(), XGate(), CNOTGate()]
model = MachineModel(
    num_qudits=qc_bqskit.num_qudits,
    gate_set=basis_gates,
)

opt_level = 1
compiled = compile(qc_bqskit, model=model, optimization_level=opt_level, seed=10)

Looking forward to your kind attention.

jagandecapri commented 5 months ago

I tested further, it seems the problem is happening when there are multiple cry gates with param 0. Hope it helps.

edyounis commented 5 months ago

This is weird. Thanks for raising the issue, I'll take a look.