Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number of bits not preserved in tk_to_qiskit conversion with RangePredicate #410

Closed
CalMacCQ opened this issue Oct 29, 2024 · 2 comments
Closed
Labels
bug Something isn't working circuit_conversion Issues and pull requests related to coverting qiskit circuits to pytket and vice versa

Comments

@CalMacCQ
Copy link
Contributor

CalMacCQ commented Oct 29, 2024

Whilst looking into #375 I found that the number of bits was not preserved in the tk_to_qiskit conversion. I think this may well be the cause of the KeyError issue.

from pytket import Circuit, OpType
from pytket.circuit.logic_exp import reg_eq
from pytket.extensions.qiskit import tk_to_qiskit


main_circ = Circuit(2, 2, "main_circ")

creg_0 = main_circ.add_c_register("creg_0", 2)

main_circ.add_gate(OpType.CH,[0, 1], condition = reg_eq(creg_0, 3))

main_circ.measure_all()
print(main_circ.n_bits) # gives 5

Image

But if I convert to a qiskit QuantumCircuit...

qc = tk_to_qiskit(main_circ)
print(qc.num_clbits) # gives 4

We only get 4 bits in the converted qiskit circuit. it seems the tk_SCRATCH_BIT[0] bit is lost in the conversion.

@CalMacCQ CalMacCQ added the bug Something isn't working label Oct 29, 2024
@CalMacCQ CalMacCQ changed the title Number of bits not preserved in tk_to_qiskit conversion Number of bits not preserved in tk_to_qiskit conversion with RangePredicate Oct 29, 2024
@CalMacCQ CalMacCQ added the circuit_conversion Issues and pull requests related to coverting qiskit circuits to pytket and vice versa label Nov 4, 2024
@yao-cqc
Copy link
Contributor

yao-cqc commented Nov 6, 2024

I think this is expected. In pytket, a scratch bit is required to store the evaluation of a range predicate, for example, if c == 2 tk_scratch_bit[0] = 1, and then condition a gate on that scratch bit, i.e., if tk_scratch_bit[0]==1 H(1). When converting such a conditional gate to qiskit, we combine these two instructions into one and remove the separate evaluation, resulting in if c == 2 H(1). Since tk_scratch_bit is no longer used, we don't add it to the qiskit circuit.

qc[0].condition

prints

(ClassicalRegister(2, 'creg_0'), 2)

suggests that the CH gate has the correct condition

@CalMacCQ CalMacCQ removed their assignment Nov 29, 2024
@CalMacCQ
Copy link
Contributor Author

Closing as I think this is expected behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working circuit_conversion Issues and pull requests related to coverting qiskit circuits to pytket and vice versa
Projects
None yet
Development

No branches or pull requests

2 participants