Skip to content

Commit

Permalink
Merge pull request #1109 from qiboteam/circuit_output_bugfix
Browse files Browse the repository at this point in the history
Bugfix to ``MeasurementOutcomes``
  • Loading branch information
renatomello authored Dec 1, 2023
2 parents 17a8523 + 93509ac commit 089e7fd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/qibo/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ def __init__(
self._frequencies = None
self._repeated_execution_frequencies = None

for gate in self.measurements:
gate.result.reset()
if samples is not None:
for m in measurements:
indices = [self.measurement_gate.qubits.index(q) for q in m.qubits]
m.result.register_samples(samples[:, indices])
else:
for gate in self.measurements:
gate.result.reset()

def frequencies(self, binary: bool = True, registers: bool = False):
"""Returns the frequencies of measured samples.
Expand Down
3 changes: 2 additions & 1 deletion tests/test_measurements_collapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
def test_measurement_collapse(backend, nqubits, targets):
initial_state = random_statevector(2**nqubits, backend=backend)
c = models.Circuit(nqubits)
for q in np.random.randint(nqubits, size=np.random.randint(nqubits, size=1)):
c.add(gates.H(q))
r = c.add(gates.M(*targets, collapse=True))
c.add(gates.M(*targets))
# final_state = backend.execute_circuit(c, np.copy(initial_state), nshots=1)[0]
outcome = backend.execute_circuit(c, np.copy(initial_state), nshots=1)
samples = r.samples()[0]
backend.assert_allclose(samples, outcome.samples()[0])
Expand Down
3 changes: 2 additions & 1 deletion tests/test_models_circuit_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def test_circuit_add_sampling(backend):

circ.add(gates.PauliNoiseChannel(0, [("Z", 0.01)]))
circ += circ_no_noise.invert()
circ.add(gates.M(0))
measurement = circ.add(gates.M(0))

# Sampling using 10 shots
np.random.seed(123)
Expand All @@ -198,6 +198,7 @@ def test_circuit_add_sampling(backend):
backend.set_seed(123)
np.random.seed(123)
for _ in range(10):
measurement.reset()
result = backend.execute_circuit(circ, nshots=1)
target_samples.append(result.samples())

Expand Down

0 comments on commit 089e7fd

Please sign in to comment.