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

Quantum circuit with conditionals can cause RunTime error #1536

Closed IlanIwumbwe closed 2 months ago

IlanIwumbwe commented 2 months ago

Benny and I found this by running a randomly generated circuit through the compiler.

Recreate with:

main_circ = Circuit(2, 2, "main_circ")
a = Symbol("a")
s_map = {a : -0.5}

# Adding creg resources 
creg_0 = main_circ.add_c_register("creg_0",2)

main_circ.Rz(1,0, condition = reg_eq(creg_0, 3))
main_circ.symbol_substitution(s_map)

main_circ.measure_all()

Results in:

AttributeError: 'pytket._tket.circuit.ClassicalOp' object has no attribute 'lower'

When we remove the symbol substitution, the code runs. After closer inspection, we found that symbol substitution changes Optype.RangePredicate ops into Optype.ClassicalOp even though optype is still stated as Optype.RangePredicate here

cqc-alec commented 2 months ago

Is the above the full code to reproduce? Running it works for me without error.

IlanIwumbwe commented 2 months ago

No, here is the updated code snippet:

main_circ = Circuit(2, 2, "main_circ")
a = Symbol("a")
s_map = {a : -0.5}

# Adding creg resources 
creg_0 = main_circ.add_c_register("creg_0",2)

main_circ.Rz(1,0, condition = reg_eq(creg_0, 3))
main_circ.symbol_substitution(s_map)

main_circ.measure_all()

backend = AerBackend()
no_pass_circ = backend.get_compiled_circuit(main_circ, optimisation_level=0)
counts = backend.run_circuit(no_pass_circ).get_counts()
cqc-alec commented 2 months ago

I think the bug here is that symbol_substitution() is implemented thus for all ClassicalOp types. But there is a whole hierarchy of types inheriting from ClassicalOp, including RangePredicateOp. So when we apply symbol_substitution to those we just get a ClassicalOp.

IlanIwumbwe commented 2 months ago

Ah, that makes sense. Thank you for the quick response.

cqc-alec commented 2 months ago

Ah, that makes sense. Thank you for the quick response.

Thank you for raising the issue. Will work on a fix.