Skip to content
New issue

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

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

Already on GitHub? # to your account

AerBackend.process_circuits fails if circuits contain different numbers of qubits #420

Closed
CalMacCQ opened this issue Nov 21, 2024 · 1 comment · Fixed by #422
Closed
Assignees
Labels
bug Something isn't working

Comments

@CalMacCQ
Copy link
Contributor

CalMacCQ commented Nov 21, 2024

Fairly minimal example provided by Blake. I'm not immediately sure what the best fix is.

from pytket import Circuit
from pytket.extensions.qiskit import AerBackend

tket_circuits = [
    Circuit(2, 2).H(0).CX(0, 1).Measure(0, 0).Measure(1, 1),
    Circuit(2, 2).H(1).CX(0, 1).Measure(0, 0).Measure(1, 1),
    Circuit(3, 3).H(0).CX(0, 1).CX(1, 2).Measure(0, 0).Measure(1, 1).Measure(2, 2),
]

aer_b = AerBackend()

print("Testing AerBackend same number of qubits")
handles = aer_b.process_circuits(tket_circuits[:2], n_shots=100)
counts = [aer_b.get_result(h).get_counts() for h in handles]

print("Testing AerBackend different number of qubits")

handles = aer_b.process_circuits(tket_circuits, n_shots=100)
counts = [aer_b.get_result(h).get_counts() for h in handles]

Output

Testing AerBackend same number of qubits
Testing AerBackend different number of qubits
Traceback (most recent call last):
  File "/Users/callum/work_projects/test-dir/.venv/lib/python3.11/site-packages/pytket/extensions/qiskit/backends/aer.py", line 328, in get_result
    return super().get_result(handle)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/callum/work_projects/test-dir/.venv/lib/python3.11/site-packages/pytket/backends/backend.py", line 347, in get_result
    raise CircuitNotRunError(handle)
pytket.backends.backend_exceptions.CircuitNotRunError: Circuit corresponding to ResultHandle('81269adf-afa7-4ee0-b4c2-bdea5844634a', 0, 2, 'null') has not been run by this backend instance.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/callum/work_projects/test-dir/support_issue.py", line 19, in <module>
    counts = [aer_b.get_result(h).get_counts() for h in handles]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/callum/work_projects/test-dir/support_issue.py", line 19, in <listcomp>
    counts = [aer_b.get_result(h).get_counts() for h in handles]
              ^^^^^^^^^^^^^^^^^^^
  File "/Users/callum/work_projects/test-dir/.venv/lib/python3.11/site-packages/pytket/extensions/qiskit/backends/aer.py", line 346, in get_result
    self._cache[ResultHandle(jobid, circ_index, qubit_n, ppc)][
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: ResultHandle('81269adf-afa7-4ee0-b4c2-bdea5844634a', 2, 2, 'null')
@CalMacCQ CalMacCQ added the bug Something isn't working label Nov 21, 2024
@Btrainwilson
Copy link

Btrainwilson commented Nov 21, 2024

I believe the issue starts in line 342 of pytket/extensions/qiskit/aer.py.

It tries super().get_result -> pytket.backend.Backend.get_result()
Which throws because "result" is not a key in self._cache[handle] for the AerBackend, as it is never assigned in line 325
Then, during the error handling in 343 onward, qubit_n is assigned (344) based on the first handle and is assumed across all handles in the loop in 352.

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

Successfully merging a pull request may close this issue.

3 participants