Skip to content

Commit

Permalink
fix symplectic matrix + test
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Mar 5, 2024
1 parent cc08cfd commit 330fd5a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/qibo/backends/clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ def execute_circuit(self, circuit, initial_state=None, nshots: int = 1000):
for gate in circuit.queue:
circuit_stim.append(gate.__class__.__name__, list(gate.qubits))

symplectic_matrix = self._stim.Tableau.from_circuit(circuit_stim)
quad_1, quad_2, quad_3, quad_4, x_phases, z_phases = (
self._stim.Tableau.from_circuit(circuit_stim).to_numpy()
)
symplectic_matrix = np.block([[quad_1, quad_2], [quad_3, quad_4]])
symplectic_matrix = np.c_[symplectic_matrix, np.r_[x_phases, z_phases]]

return Clifford(
symplectic_matrix,
Expand Down
30 changes: 24 additions & 6 deletions tests/test_backends_clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,20 @@ def test_set_backend(backend):
def test_noise_channels(backend):
clifford_bkd = construct_clifford_backend(backend)

nqubits = 3
c = random_clifford(nqubits, backend=backend)
c.density_matrix = True
c_copy = c.copy()
c.add(gates.M(*range(nqubits)))
c_copy.add(gates.M(*range(nqubits)))
noise = NoiseModel()
noise.add(PauliError([("X", 0.5)]), gates.X)
noise.add(DepolarizingError(0.1), gates.CZ)

nqubits = 3

c = random_clifford(nqubits, density_matrix=True, backend=backend)
c.add(gates.M(*range(nqubits)))

c_copy = c.copy()

c = noise.apply(c)
c_copy = noise.apply(c_copy)

numpy_result = numpy_bkd.execute_circuit(c)
clifford_result = clifford_bkd.execute_circuit(c_copy)

Expand All @@ -245,3 +248,18 @@ def test_noise_channels(backend):
clifford_result.probabilities(),
atol=1e-1,
)


def test_stim(backend):
clifford_bkd = construct_clifford_backend(backend)
clifford_stim = CliffordBackend(engine="stim")

nqubits = 3
circuit = random_clifford(nqubits, backend=backend)

result_qibo = clifford_bkd.execute_circuit(circuit)
result_stim = clifford_stim.execute_circuit(circuit)

backend.assert_allclose(
result_stim.symplectic_matrix, result_qibo.symplectic_matrix
)

0 comments on commit 330fd5a

Please sign in to comment.