CQCL / tket

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

Improve error message for mismatching unit ids when adding conditional gates #1580

Open CalMacCQ opened 2 months ago

CalMacCQ commented 2 months ago

The following method works fine for adding a condtional-X gate to a Circuit.

from pytket import Circuit

circ = Circuit()
q = circ.add_q_register("q", 1) # default "q" register
a = circ.add_c_register("a", 1)

circ.X(q[0], condition_bits=[a[0]], condition_value=1)

Notice how I am passing the Qubit q[0] into the Circuit.add_gate method.

However if I replace q[0] with 0....

from pytket import Circuit

circ = Circuit()
q = circ.add_q_register("q", 1) # default "q" register
a = circ.add_c_register("a", 1)

circ.X(0, condition_bits=[a[0]], condition_value=1)

I get the following RuntimeError

--
RuntimeError                              Traceback (most recent call last)
Cell In[16], line 1
----> 1 circ.X(0, condition_bits=[a[0]], condition_value=1)

RuntimeError: Unable to cast Python instance of type <class 'list'> to C++ type 'std::__1::vector<unsigned int, std::__1::allocator<unsigned int>>'

Its not at all obvoius from this error what has gone wrong here. This error message should make the fix clearer.

In fact, arguably this shouldn't give an error in pytket as in the current interface we support referencing qubits with Qubits and integers. However not sure how practical overload merging would be.