Skip to content

Commit

Permalink
Fix conversion of ClOp.BitEq and ClOp.BitNeq to QASM (#1754)
Browse files Browse the repository at this point in the history
cqc-alec authored Jan 24, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 364fcba commit 7eaf926
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pytket/binders/circuit/clexpr.cpp
Original file line number Diff line number Diff line change
@@ -149,17 +149,19 @@ static std::string qasm_expr_repr(
if (n_args != 2) {
throw std::logic_error("BitEq with != 2 arguments");
}
ss << "~(";
ss << qasm_arg_repr(args[0], input_bits, input_regs, ArgValueType::Bit);
ss << " == ";
ss << " ^ ";
ss << qasm_arg_repr(args[1], input_bits, input_regs, ArgValueType::Bit);
ss << ")";
break;

case ClOp::BitNeq:
if (n_args != 2) {
throw std::logic_error("BitNeq with != 2 arguments");
}
ss << qasm_arg_repr(args[0], input_bits, input_regs, ArgValueType::Bit)
<< " != "
<< " ^ "
<< qasm_arg_repr(args[1], input_bits, input_regs, ArgValueType::Bit);
break;

1 change: 1 addition & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ Fixes:

* Make `CliffordCircuitPredicate` accept circuits containing measurements.
* Make `DecomposeBoxes` pass recurse into conditional boxes.
* Fix conversion of `ClOp.BitEq` and `ClOp.BitNeq` to QASM.

1.39.0 (January 2025)
---------------------
29 changes: 29 additions & 0 deletions pytket/tests/clexpr_test.py
Original file line number Diff line number Diff line change
@@ -255,3 +255,32 @@ def test_add_logicexp_as_clexpr() -> None:
c = (a | b);
"""
)


def test_biteq_bitneq_to_qasm() -> None:
# https://github.com/CQCL/tket/issues/1753
c = Circuit(0, 3)
c.add_clexpr(
WiredClExpr(
expr=ClExpr(
op=ClOp.BitAnd,
args=[
ClExpr(op=ClOp.BitEq, args=[ClBitVar(0), 0]),
ClExpr(op=ClOp.BitNeq, args=[ClBitVar(1), 1]),
],
),
bit_posn={0: 0, 1: 1},
output_posn=[2],
),
c.bits,
)
qasm = circuit_to_qasm_str(c, header="hqslib1")
assert (
qasm
== """OPENQASM 2.0;
include "hqslib1.inc";
creg c[3];
c[2] = ((~(c[0] ^ 0)) & (c[1] ^ 1));
"""
)

0 comments on commit 7eaf926

Please sign in to comment.