Skip to content

Commit cc87d33

Browse files
authoredMar 30, 2022
Merge pull request #564 from qiboteam/fixadd
Fix sampling when using `circuit.add()`
2 parents e66224d + ed0f9e7 commit cc87d33

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
 

‎src/qibo/abstractions/circuit.py

+4
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ def __add__(self, circuit):
148148
newcircuit.check_measured(v)
149149
newcircuit.measurement_tuples[k] = v
150150
newcircuit.measurement_gate.add(circuit.measurement_gate)
151+
152+
# Re-execute full circuit when sampling if one of the circuit has repeated_execution True
153+
newcircuit.repeated_execution = self.repeated_execution or circuit.repeated_execution
154+
151155
return newcircuit
152156

153157
def on_qubits(self, *q):

‎src/qibo/tests/test_core_circuit_noise.py

+34
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,37 @@ def test_density_matrix_circuit_errors():
160160
c = Circuit(5)
161161
with pytest.raises(ValueError):
162162
c.add(gates.KrausChannel([((0,), np.eye(2))]))
163+
164+
165+
def test_circuit_add_sampling(backend):
166+
"""Check measurements when simulating added circuits with noise"""
167+
# Create random noisy circuit and add noiseless inverted circuit
168+
gates_set = [gates.X, gates.Y, gates.Z, gates.H, gates.S, gates.SDG, gates.I]
169+
circ = Circuit(1)
170+
circ_no_noise = Circuit(1)
171+
172+
for _ in range(10):
173+
new_gate = np.random.choice(gates_set)(0)
174+
circ.add(gates.PauliNoiseChannel(0, pz=0.01))
175+
circ.add(new_gate)
176+
circ_no_noise.add(new_gate)
177+
178+
circ.add(gates.PauliNoiseChannel(0, pz=0.01))
179+
circ += circ_no_noise.invert()
180+
circ.add(gates.M(0))
181+
182+
# Sampling using 10 shots
183+
np.random.seed(123)
184+
K.set_seed(123)
185+
samples = circ(nshots=10).samples()
186+
187+
# Sampling using 1 shot in for loop
188+
target_samples = []
189+
K.set_seed(123)
190+
np.random.seed(123)
191+
for _ in range(10):
192+
target_samples.append(circ(nshots=1).samples())
193+
194+
target_samples = np.stack(target_samples)
195+
196+
K.assert_allclose(samples, target_samples[:, 0])

0 commit comments

Comments
 (0)
Please sign in to comment.