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

Fix Sampler with multiple classical registers #1680

Merged
merged 2 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qiskit_aer/primitives/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _call(
shots = sum(counts.values())
quasis.append(
QuasiDistribution(
{k: v / shots for k, v in counts.items()},
{k.replace(" ", ""): v / shots for k, v in counts.items()},
shots=shots,
)
)
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/fix-split-cregs-5b5494a92c4903e7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features:
- |
Fix the bug where the :class:`.Sampler` fails if the input circuit has multiple classical
registers.
15 changes: 14 additions & 1 deletion test/terra/primitives/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import numpy as np
from ddt import data, ddt
from qiskit import QuantumCircuit
from qiskit import ClassicalRegister, QuantumCircuit
from qiskit.circuit import Parameter
from qiskit.circuit.library import RealAmplitudes
from qiskit.exceptions import QiskitError
Expand Down Expand Up @@ -537,6 +537,19 @@ def test_num_clbits(self, shots):
bin_probs = quasis.binary_probabilities()
self.assertDictAlmostEqual(bin_probs, {"0000": 0.5, "0001": 0.5}, delta=1e-2)

def test_multiple_cregs(self):
"""Test for the circuit with multipe cregs"""
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)

result = Sampler().run(qc, shots=100).result()
self.assertDictAlmostEqual(result.quasi_dists[0], {0: 1})


if __name__ == "__main__":
unittest.main()