diff --git a/src/qibo/backends/clifford.py b/src/qibo/backends/clifford.py index d1a1586f6e..b79842ad1e 100644 --- a/src/qibo/backends/clifford.py +++ b/src/qibo/backends/clifford.py @@ -85,8 +85,19 @@ def zero_state(self, nqubits: int): symplectic_matrix[nqubits:-1, nqubits : 2 * nqubits] = self.np.copy(I) return symplectic_matrix - def reshape_clifford_state(self, state, nqubits): - """Reshape the symplectic matrix to the shape needed by the engine. + def clifford_pre_execution_reshape(self, state): + """Reshape the symplectic matrix to the shape needed by the engine before circuit execution. + + Args: + state (ndarray): The input state. + + Returns: + (ndarray): The reshaped state. + """ + return self.engine.clifford_pre_execution_reshape(state) + + def clifford_post_execution_reshape(self, state, nqubits): + """Reshape the symplectic matrix to the shape needed by the engine after circuit execution. Args: state (ndarray): The input state. @@ -95,7 +106,7 @@ def reshape_clifford_state(self, state, nqubits): Returns: (ndarray): The reshaped state. """ - return self.engine.reshape_clifford_state(state, nqubits) + return self.engine.clifford_post_execution_reshape(state, nqubits) def apply_gate_clifford(self, gate, symplectic_matrix, nqubits): operation = getattr(self.clifford_operations, gate.__class__.__name__) @@ -132,12 +143,12 @@ def execute_circuit(self, circuit, initial_state=None, nshots: int = 1000): state = self.zero_state(nqubits) if initial_state is None else initial_state - state = self.reshape_clifford_state(state, nqubits) + state = self.clifford_pre_execution_reshape(state) for gate in circuit.queue: state = gate.apply_clifford(self, state, nqubits) - state = self.reshape_clifford_state(state, nqubits) + state = self.clifford_post_execution_reshape(state, nqubits) clifford = Clifford( state, @@ -220,6 +231,8 @@ def sample_shots( if isinstance(qubits, list): qubits = tuple(qubits) + state = self.clifford_pre_execution_reshape(state) + if collapse: samples = [ self.clifford_operations.M(state, qubits, nqubits) diff --git a/src/qibo/backends/clifford_operations.py b/src/qibo/backends/clifford_operations.py index 273256c2ae..2e52326c13 100644 --- a/src/qibo/backends/clifford_operations.py +++ b/src/qibo/backends/clifford_operations.py @@ -404,7 +404,7 @@ def _random_outcome(state, p, q, nqubits): if h.shape[0] > 0: state = _rowsum( state, - h, + h.astype(np.uint), p * np.ones(h.shape[0], dtype=np.uint), nqubits, ) diff --git a/src/qibo/backends/numpy.py b/src/qibo/backends/numpy.py index c2a4d209a3..2e9cc18937 100644 --- a/src/qibo/backends/numpy.py +++ b/src/qibo/backends/numpy.py @@ -77,7 +77,10 @@ def to_numpy(self, x): def compile(self, func): return func - def reshape_clifford_state(self, state, nqubits): + def clifford_pre_execution_reshape(self, state): + return state + + def clifford_post_execution_reshape(self, state, nqubits): return state def zero_state(self, nqubits):