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

Error in executing StateTomography jobs on real device #1060

Closed
HongyeY opened this issue Feb 21, 2023 · 2 comments · Fixed by #1059
Closed

Error in executing StateTomography jobs on real device #1060

HongyeY opened this issue Feb 21, 2023 · 2 comments · Fixed by #1059

Comments

@HongyeY
Copy link

HongyeY commented Feb 21, 2023

Information

  • qiskit-ibm-provider version: 0.3.0
  • Python version: 3.10.6
  • Operating system: Windows 11

In addition, I'm using the latest version of qiskit_experiments on github with qiskit 0.41.0.

What is the current behavior?

I have an error when running StateTomography job from the qiskit_experiments library. The simulators work well but jobs from real backends keep failing. The error message indicates some I/O errors:

jobs and analysis raised exceptions [Experiment ID: b06fddb7-ffd0-48c4-8420-bc27ed8d5724]:Traceback (most recent call last):

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_experiments\framework\experiment_data.py", line 791, in _add_job_data
    job_result = job.result()

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\job\ibm_circuit_job.py", line 247, in result
    self.wait_for_final_state(timeout=timeout)

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\job\ibm_circuit_job.py", line 635, in wait_for_final_state
    status = self.status()

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\job\ibm_circuit_job.py", line 355, in status
    api_response = self._runtime_client.job_get(self.job_id())["state"]

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\api\clients\runtime.py", line 159, in job_get
    response = self._api.program_job(job_id).get()

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\api\rest\program_job.py", line 56, in get
    return self.session.get(self.get_url("self")).json(cls=RuntimeDecoder)

  File "c:\Python\Python310\lib\site-packages\requests\models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)

  File "c:\Python\Python310\lib\json\__init__.py", line 359, in loads
    return cls(**kw).decode(s)

  File "c:\Python\Python310\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

  File "c:\Python\Python310\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\utils\json.py", line 296, in object_hook
    return _decode_and_deserialize(obj_val, load)[0]

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\utils\json.py", line 126, in _decode_and_deserialize
    return deserializer(buff)

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\qpy\interface.py", line 278, in load
    loader(  # type: ignore[no-untyped-call]

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\qpy\binary_io\circuits.py", line 1012, in read_circuit
    _read_instruction(

  File "C:\Users\...\AppData\Roaming\Python\Python310\site-packages\qiskit_ibm_provider\qpy\binary_io\circuits.py", line 214, in _read_instruction
    struct.unpack(

struct.error: unpack requires a buffer of 5 bytes

Steps to reproduce the problem

  1. get the backend
provider = IBMProvider()
backend_auckland = provider.get_backend('ibm_auckland', instance='...')
  1. construct an arbitratry circuit
qNum=6
qr = QuantumRegister(qNum)
cr = ClassicalRegister(qNum)
qc = QuantumCircuit(qr,cr)

qc.h(range(qNum))
for i in range(0,qNum-1,2):
    qc.cz(i,i+1)
for i in range(1,qNum-1,2):
    qc.cz(i,i+1)
  1. run the state tomography job
from qiskit_experiments.library import StateTomography
initial_layout=[0,1,2,3,5,8]
qstexp = StateTomography(qc,physical_qubits=initial_layout,measurement_indices=[0,1,2])
job=qstexp.run(backend=backend_auckland, shots=32000)
@spratapsi
Copy link

spratapsi commented Feb 27, 2023

I am having a similar issue with the ProcessTomography experiment.

It seems that the circuits are breaking the qpy encoding/decoding.

Minimal example that fails

from qiskit import QuantumCircuit
from qiskit_experiments.library import StateTomography
from qiskit_ibm_provider import qpy

def encode_decode(qc):
    with open('test.qpy', 'wb') as fd:
        qpy.dump(qc, fd)
    with open('test.qpy', 'rb') as fd:
        new_qc = qpy.load(fd)[0]
    return new_qc

# This throws an Exception
qc = QuantumCircuit(2)
tomography = ProcessTomography(circuit=qc)
qc_ = tomography.circuits()[0]
encode_decode(qc_)

The root cause
The problem seems to come from passing the tuples self._prep_indices and self._meas_indices to QuantumCircuit.barrier, in lines 224 and 234 of the definition of TomographyExperiment.circuits.

Here is a simple circuit with the same behaviour.

_prep_indices = (0, 1)

# Fails -- passing a tuple to barrier
qc = QuantumCircuit(2)
qc.barrier(_prep_indices)
encode_decode(qc)

Possible fix
I don't know if this bug belongs here or in the qiskit-experiments repository. If it's the latter, the problem can be solved by unpacking the tuples or converting them to lists.

# Proposed solution 1 -- unpack tuple
qc = QuantumCircuit(2)
qc.barrier(*_prep_indices)
encode_decode(qc)

# Proposed solution 2 -- convert to list
qc = QuantumCircuit(2)
qc.barrier(list(_prep_indices))
encode_decode(qc)

@wshanks
Copy link
Collaborator

wshanks commented Feb 28, 2023

Thanks, @spratapsi! I would say that this does belong in qiskit-experiments. I will make PR soon to fix this in tomography_experiment.py.

wshanks referenced this issue in wshanks/qiskit-experiments Feb 28, 2023
`QuantumCircuit.barrier()` accepts many forms of input, but `tuple` is
not one of them. The prep and measurement indices passed as tuples to
the barrier method were causing qpy serialization/deserialization to
fail. It is possible that there were other unnoticed consequences to the
type of the barrier argument being incorrect.

Closes https://github.com/Qiskit/qiskit-ibm-provider/issues/523
@mtreinish mtreinish transferred this issue from Qiskit/qiskit-ibm-provider Feb 28, 2023
coruscating pushed a commit that referenced this issue Mar 2, 2023
<!--
⚠️ If you do not respect this template, your pull request will be
closed.
⚠️ Your pull request title should be short detailed and understandable
for all.
⚠️ Also, please add it in the CHANGELOG file under Unreleased section.
⚠️ If your pull request fixes an open issue, please link to the issue.

✅ I have added the tests to cover my changes.
✅ I have updated the documentation accordingly.
✅ I have read the CONTRIBUTING document.
-->

### Summary

Fix barrier instructions in tomography experiments

### Details and comments

`QuantumCircuit.barrier()` accepts many forms of input, but `tuple` is
not one of them. The prep and measurement indices passed as tuples to
the barrier method were causing qpy serialization/deserialization to
fail. It is possible that there were other unnoticed consequences to the
type of the barrier argument being incorrect.

Closes #1060.

See also [this Slack
thread](https://qiskit.slack.com/archives/CGZDF48EN/p1676934228659149).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants