CQCL / tket

Source code for the TKET quantum compiler, Python bindings and utilities
https://docs.quantinuum.com/tket/
Apache License 2.0
256 stars 48 forks source link

Syntax Error in Exported QASM for `rccx` Gate #1325

Open p51lee opened 7 months ago

p51lee commented 7 months ago

Overview

Exporting a qasm code that includes an rccx gate using circuit_to_qasm_str function results in a syntax error.

Example and reproduction steps

Consider the following Python code:

from pytket.qasm import circuit_from_qasm_str, circuit_to_qasm_str

qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];

rccx q[0],q[1],q[2];
"""

circuit_tket = circuit_from_qasm_str(qasm)
print(circuit_to_qasm_str(circuit_tket))

Observed

The resulting QASM code contains an unclosed parenthesis:

OPENQASM 2.0;
include "qelib1.inc";

qreg q[3];
creg c[1];
rccx( q[0],q[1],q[2];  // Incorrect syntax

As defined in qelib1.inc, the last line should be rccx q[0],q[1],q[2];.

...
gate rccx a,b,c
{
  u2(0,pi) c;
  u1(pi/4) c;
  cx b, c;
  u1(-pi/4) c;
  cx a, c;
  u1(pi/4) c;
  cx b, c;
  u1(-pi/4) c;
  u2(0,pi) c;
}
...
p51lee commented 7 months ago

The same issue occurs with the c3sqrtx gate.

cqc-alec commented 6 months ago

Oops. I'll take a look.

cqc-alec commented 6 months ago

With #1300 the behaviour changed so that all boxes are decomposed before conversion to QASM. This makes the resulting QASM correct, though more verbose:

OPENQASM 2.0;
include "qelib1.inc";

qreg q[3];
u3(0.5*pi,0.0*pi,1.0*pi) q[2];
u3(0.0*pi,0.0*pi,0.25*pi) q[2];
cx q[1],q[2];
u3(0.0*pi,0.0*pi,1.75*pi) q[2];
cx q[0],q[2];
u3(0.0*pi,0.0*pi,0.25*pi) q[2];
cx q[1],q[2];
u3(0.0*pi,0.0*pi,1.75*pi) q[2];
u3(0.5*pi,0.0*pi,1.0*pi) q[2];
cqc-alec commented 6 months ago

Maybe decomposing all boxes was the wrong idea for that fix. Will have a think if there is a better way.

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale.