Skip to content

Commit

Permalink
fix qasm_simulator measure sampling (#2735)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic authored and ajavadia committed Jul 3, 2019
1 parent 6226161 commit 4679413
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -100,6 +100,8 @@ The format is based on [Keep a Changelog].
- Fixes a bug that removed `id` gates from circuit. id gates are
like a `wait` command and will never be removed (\#2663)
- Fixed bug in CommutationAnalysis pass affecting conditional gates (\#2669)
- Fixed bug in measure sampling for BasicAer Qasm simulator if a qubit
was measured into more than one memory cbit (\#2735)


## [0.8.2] - 2019-06-14
Expand Down
4 changes: 2 additions & 2 deletions qiskit/providers/basicaer/qasm_simulator.py
Expand Up @@ -202,8 +202,8 @@ def _add_sample_measure(self, measure_params, num_samples):
memory = []
for sample in samples:
classical_memory = self._classical_memory
for count, (qubit, cmembit) in enumerate(sorted(measure_params)):
qubit_outcome = int((sample & (1 << count)) >> count)
for qubit, cmembit in measure_params:
qubit_outcome = int((sample & (1 << qubit)) >> qubit)
membit = 1 << cmembit
classical_memory = (classical_memory & (~membit)) | (qubit_outcome << cmembit)
value = bin(classical_memory)[2:]
Expand Down
21 changes: 21 additions & 0 deletions test/python/basicaer/test_qasm_simulator.py
Expand Up @@ -48,6 +48,27 @@ def test_qasm_simulator_single_shot(self):
result = self.backend.run(self.qobj).result()
self.assertEqual(result.success, True)

def test_qasm_simulator_measure_sampler(self):
"""Test measure sampler if qubits measured more than once."""
shots = 100
qr = QuantumRegister(2, 'qr')
cr = ClassicalRegister(4, 'cr')
circuit = QuantumCircuit(qr, cr)
circuit.x(qr[1])
circuit.measure(qr[0], cr[0])
circuit.measure(qr[1], cr[1])
circuit.measure(qr[1], cr[2])
circuit.measure(qr[0], cr[3])
target = {'0110': shots}
job = execute(
circuit,
backend=self.backend,
shots=shots,
seed_simulator=self.seed)
result = job.result()
counts = result.get_counts(0)
self.assertEqual(counts, target)

def test_qasm_simulator(self):
"""Test data counts output for single circuit run against reference."""
result = self.backend.run(self.qobj).result()
Expand Down

0 comments on commit 4679413

Please sign in to comment.