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

Incorrect simulation of nested classical controls on the AerBackend #442

Open
dandanua opened this issue Dec 23, 2024 · 6 comments
Open
Labels
bug Something isn't working

Comments

@dandanua
Copy link

dandanua commented Dec 23, 2024

I was trying a fairly simple test of nested classical controls in quantinuum jupyter lab and ran into an issue.

Consider a setup with 3 qubits and 3 classical registers. On the first qubit we use the H gate and then prepare a Bell state on the other two qubits using a standard circuit controlled by the state of the first qubit. However, when the Bell state preparation is done using an equivalent classically controlled operation (well, not quite equivalent, but the simulation result should be the same), the simulation result of the whole setup is incorrect. Here is the code:

from pytket.circuit import *
from pytket.circuit.display import render_circuit_jupyter as draw
from pytket.extensions.qiskit import AerBackend, AerStateBackend
from pytket.utils import probs_from_counts

U0 = Circuit(2, 2)
U0.H(0)
# ------ 
# The use of CX version below works fine and returns the correct result in the end
# U0.CX(0, 1)
# U0.measure_all()
# ---------
# The classical control version fails
U0.Measure(0, 0)
U0.X(1, condition_bits=[0], condition_value=1)
U0.Measure(1, 1)

g_U0 = CustomGateDef.define("u0", U0, [])

U = Circuit()
q = U.add_q_register("q", 3)
c = U.add_c_register("c", 3)
U.H(0)
U.Measure(0, 0)
U.add_custom_gate(g_U0, [], [1, 2, 1, 2], condition=if_not_bit(c[0]))

backend = AerBackend()
c_U = backend.get_compiled_circuit(U)

handle = backend.process_circuit(c_U, n_shots=1000)
counts = backend.get_result(handle).get_counts()
print(counts)
print(probs_from_counts(counts))

which returns

{(0, 0, 1): 0.258, (0, 1, 1): 0.243, (1, 0, 0): 0.499}

while it should return

{(0, 0, 0): 0.256, (0, 1, 1): 0.253, (1, 0, 0): 0.491}

as in the case of using CX in the custom nested gate.

@CalMacCQ
Copy link
Contributor

CalMacCQ commented Dec 23, 2024

Hi @dandanua, Thank you for raising an issue. We'll take a look and get back to you.

Can I ask which version of pytket/pytket-qiskit you are using?

@dandanua
Copy link
Author

@CalMacCQ

Hi, it's the one that Quantinuum Nexus is using in their Jupyter Lab, within the standard Python kernel. I don't know how to check the version number from there.

@CalMacCQ
Copy link
Contributor

CalMacCQ commented Dec 23, 2024

Ah thanks, good to know. thats enough for me. I'll dig into the issue soon.

I think you should just be able to do a ! followed by the shell command in a python notebook cell to find package verions.

! pip show pytket-qiskit 

@CalMacCQ CalMacCQ added the bug Something isn't working label Dec 23, 2024
@dandanua
Copy link
Author

Version: 0.56.0

BTW, I've tried to run the same circuit against the H1-Emulator using the "getting-started" notebook workflow. The compilation job ran successfully and produced some transformed circuit but the execution job has failed with the error

JobError: Job errored with detail: <class 'pytket.backends.backend_exceptions.CircuitNotValidError'>: Circuit with index 0 in submitted does not satisfy GateSetPredicate:{ TK2 ExplicitPredicate CopyBits ClExpr WASM ClassicalExpBox ZZMax ZZPhase Barrier MultiBit PhasedX ExplicitModifier Rz RangePredicate Reset SetBits Measure } (try compiling with backend.get_compiled_circuits first).

I'm not sure how to fix this, since I already execute the compiled circuit as in the guide.

@CalMacCQ
Copy link
Contributor

CalMacCQ commented Dec 23, 2024

BTW, I've tried to run the same circuit against the H1-Emulator using the "getting-started" notebook workflow. The compilation job ran successfully and produced some transformed circuit but the execution job has failed with the error

hmm strange, I'll make a note to look into that as well as the issue above. The transformed circuit link seems not to work for me.

@dandanua
Copy link
Author

You must have blocked access to imgur. Here is another link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants