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

AerSampler fails with a circuit and split registers #1679

Closed
t-imamichi opened this issue Dec 7, 2022 · 1 comment · Fixed by #1680
Closed

AerSampler fails with a circuit and split registers #1679

t-imamichi opened this issue Dec 7, 2022 · 1 comment · Fixed by #1680
Labels
bug Something isn't working

Comments

@t-imamichi
Copy link
Member

Informations

  • Qiskit Aer version: 0.11.2
  • Python version: 3.9.15
  • Operating system: 12.6.1

What is the current behavior?

#1670 raises an error with a circuit and split registers as follows.

from qiskit import QuantumCircuit, ClassicalRegister
from qiskit.primitives import Sampler as RefSampler
from qiskit_aer.primitives import Sampler as AerSampler

qc = QuantumCircuit(2)
cr1 = ClassicalRegister(1, 'cr1')
cr2 = ClassicalRegister(1, 'cr2')
qc.add_register(cr1)
qc.add_register(cr2)
qc.measure(0, 0)
qc.measure(1, 1)

for sampler in [RefSampler(), AerSampler()]:
    print(sampler.run(qc, shots=100).result())

aer 0.11.1

<qiskit.primitives.sampler.Sampler object at 0x11b93abb0>
SamplerResult(quasi_dists=[{0: 1.0, 1: 0.0, 2: 0.0, 3: 0.0}], metadata=[{'shots': 100}])

<qiskit_aer.primitives.sampler.Sampler object at 0x11b93ab80>
SamplerResult(quasi_dists=[{0: 1.0}], metadata=[{'shots': 100, 'simulator_metadata': {'parallel_state_update': 1, 'parallel_shots': 1, 'sample_measure_time': 9.0047e-05, 'noise': 'ideal', 'batched_shots_optimization': False, 'remapped_qubits': False, 'device': 'CPU', 'active_input_qubits': [0, 1], 'measure_sampling': True, 'num_clbits': 2, 'input_qubit_map': [[1, 1], [0, 0]], 'num_qubits': 2, 'method': 'stabilizer', 'fusion': {'enabled': False}}}])

aer 0.11.2

<qiskit.primitives.sampler.Sampler object at 0x113c32dc0>
SamplerResult(quasi_dists=[{0: 1.0, 1: 0.0, 2: 0.0, 3: 0.0}], metadata=[{'shots': 100}])

<qiskit_aer.primitives.sampler.Sampler object at 0x113c32e20>
Traceback (most recent call last):
  File "/Users/ima/tasks/2_2022/qiskit/aer/tmp/split_cr.py", line 17, in <module>
    print(sampler.run(qc, shots=100).result())
  File "/Users/ima/envs/dev39/lib/python3.9/site-packages/qiskit/primitives/primitive_job.py", line 50, in result
    return self._future.result()
  File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/_base.py", line 439, in result
    return self.__get_result()
  File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/_base.py", line 391, in __get_result
    raise self._exception
  File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/ima/envs/dev39/lib/python3.9/site-packages/qiskit_aer/primitives/sampler.py", line 138, in _call
    QuasiDistribution(
  File "/Users/ima/envs/dev39/lib/python3.9/site-packages/qiskit/result/distributions/quasi.py", line 71, in __init__
    raise ValueError(
ValueError: The input keys are not a valid string format, must either be a hex string prefixed by '0x' or a binary string optionally prefixed with 0b

Steps to reproduce the problem

What is the expected behavior?

No error

Suggested solutions

The main reason is Terra's QuasiDistribution cannot accept bitstrings with whitespaces due to split registers.
There are three opitions.

  1. Reverts Fix SamplerResult num_clbits #1670
  2. Revises QuasiDistribution to accept bitstrings with white spaces
  3. Removes white spaces from bitstrings as follows.
    {k: v / shots for k, v in counts.items()},
{k.replace(" ", ""): v / shots for k, v in counts.items()},
@ikkoham
Copy link
Collaborator

ikkoham commented Dec 7, 2022

Thank you. I'll fix this until next release (0.12).

ikkoham added a commit to ikkoham/qiskit-aer that referenced this issue Dec 8, 2022
@mergify mergify bot closed this as completed in #1680 Jan 6, 2023
mergify bot pushed a commit that referenced this issue Jan 6, 2023
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

Successfully merging a pull request may close this issue.

2 participants