Hubert
Dreexis

3

Posted

QASM2 Circuit Optimization with PyZX: Handling 'if' and 'reset' Errors

Hello everyone,
I’m currently working on circuit optimization using the pyzx library. My goal is to take a QASM2 circuit as input, optimize it, and return the optimized circuit.
However, I'm encountering issues with certain QASM2 grammar elements, particularly "if" and "reset" statements. I get the following error:
TypeError: Invalid specification c0==1
I'm trying to determine whether this issue is due to the pyzx library not fully supporting the QASM2 grammar or if there's something wrong with my approach.
Here is the function I’ve written to optimize the circuit:
def optimize(circuit: str) -> str: """ Optimize a circuit. @param: circuit to optimize in QASM2 @return: optimized circuit. """
c = zx.Circuit.from_qasm(circuit)
g = c.to_graph()
zx.full_reduce(g, quiet=True)
c_opt = zx.extract_circuit(g.copy())
int_str = zx.Circuit.to_qasm(c_opt)
return int_str
And here is an example of a circuit that leads to the error:
OPENQASM 2.0; include "qelib1.inc"; qreg q[18]; creg c[14]; h q[0]; h q[1]; h q[2]; h q[3]; h q[4]; h q[5]; cx q[0],q[6]; cx q[1],q[8]; cx q[2],q[10]; cx q[3],q[6]; cx q[3],q[7]; cx q[4],q[8]; cx q[4],q[9]; cx q[5],q[10]; cx q[5],q[11]; x q[6]; x q[8]; x q[9]; x q[10]; x q[11]; reset q[13]; reset q[14]; reset q[15]; reset q[16]; reset q[17]; measure q[12] -> c[12]; measure q[0] -> c[0]; measure q[1] -> c[1]; measure q[2] -> c[2]; measure q[3] -> c[3]; measure q[4] -> c[4]; measure q[5] -> c[5]; measure q[6] -> c[6]; measure q[7] -> c[7]; measure q[8] -> c[8]; measure q[9] -> c[9]; measure q[10] -> c[10]; measure q[11] -> c[11];
Is this a limitation of the pyzx library in handling the full QASM2 grammar, or am I missing something in the code?
Any guidance or suggestions would be greatly appreciated! Thank you in advance.

Order by:

Jannes Stubbemann

3

Posted by stubbi 9 days ago (edited)

PyZX does not support if and reset statements in QASM

Hubert

1

Posted by Dreexis 10 hours ago

Thanks for this info !!!
Do you know if they will be implementing it in the future or if I need to check on other libraries for this ?

Want to join this discussion?

Join our community today and start discussing with our members by participating in exciting events, competitions, and challenges. Sign up now to engage with quantum experts!