Skip to content

Commit

Permalink
Test vs qiskit
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Nov 20, 2023
1 parent 24f978f commit ce8fd23
Show file tree
Hide file tree
Showing 11 changed files with 854 additions and 531 deletions.
10 changes: 6 additions & 4 deletions src/stim/circuit/circuit.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1386,14 +1386,14 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
return out.str();
},
pybind11::kw_only(),
pybind11::arg("open_qasm_version") = 3,
pybind11::arg("open_qasm_version"),
pybind11::arg("skip_dets_and_obs") = false,
clean_doc_string(R"DOC(
@signature def to_qasm(self, *, open_qasm_version: int, skip_dets_and_obs: bool = False) -> str:
Creates an equivalent OpenQASM implementation of the circuit.
Args:
open_qasm_version: Defaults to 3. The version of OpenQASM to target.
open_qasm_version: The version of OpenQASM to target.
This should be set to 2 or to 3.
Differences between the versions are:
Expand Down Expand Up @@ -1422,12 +1422,13 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
>>> import stim
>>> circuit = stim.Circuit("""
... R 0 1
... X 0
... H 0
... CX 0 1
... M 0 1
... DETECTOR rec[-1] rec[-2]
... """);
>>> qasm = circuit.to_qasm();
>>> qasm = circuit.to_qasm(open_qasm_version=3);
>>> print(qasm.strip().replace('\n\n', '\n'))
OPENQASM 3.0;
include "stdgates.inc";
Expand All @@ -1436,11 +1437,12 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
bit dets[1];
reset q[0];
reset q[1];
x q[0];
h q[0];
cx q[0], q[1];
measure q[0] -> rec[0];
measure q[1] -> rec[1];
dets[0] = rec[1] ^ rec[0] ^ 0;
dets[0] = rec[1] ^ rec[0] ^ 1;
)DOC")
.data());

Expand Down
13 changes: 13 additions & 0 deletions src/stim/circuit/circuit.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,19 @@ Circuit stim::generate_test_circuit_with_all_operations() {
OBSERVABLE_INCLUDE(0) rec[-1]
MPAD 0 1 0
TICK
# Inverted measurements.
MRX !0
MY !1
MZZ !2 3
MYY !4 !5
MPP X6*!Y7*Z8
TICK
# Feedback
CX rec[-1] 0
CY sweep[0] 1
CZ 2 rec[-1]
)CIRCUIT");
}

Expand Down
60 changes: 0 additions & 60 deletions src/stim/circuit/circuit_pybind_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,63 +1573,3 @@ def test_detslice_filter_coords_flexibility():
assert str(d1) == str(d3)
assert str(d1) == str(d4)
assert str(d1) == str(d5)


def test_to_qasm():
c = stim.Circuit("""
RX 0 1
TICK
H 1
CX 0 1
TICK
M 0 1
DETECTOR rec[-1] rec[-2]
C_XYZ 0
""")

assert c.to_qasm().strip() == """
OPENQASM 3.0;
include "stdgates.inc";
gate cxyz q0 { U(pi/2, 0, pi/2) q0; }
def rx(qubit q0) { reset q0; h q0; }
qubit q[2];
bit m[2];
bit dets[1];
rx(q[0]);
rx(q[1]);
barrier q;
h q[1];
cx q[0], q[1];
barrier q;
measure q[0] -> m[0];
measure q[1] -> m[1];
dets[0] = rec[1] ^ rec[0] ^ 0;
cxyz q[0];
""".strip()

assert c.to_qasm(open_qasm_version=2, skip_dets_and_obs=True).strip() == """
OPENQASM 2.0;
include "qelib1.inc";
gate cxyz q0 { U(pi/2, 0, pi/2) q0; }
qreg q[2];
creg m[2];
reset q[0]; h q[0]; // decomposed RX
reset q[1]; h q[1]; // decomposed RX
barrier q;
h q[1];
cx q[0], q[1];
barrier q;
measure q[0] -> m[0];
measure q[1] -> m[1];
cxyz q[0];
""".strip()
Loading

0 comments on commit ce8fd23

Please sign in to comment.