Skip to content

Commit

Permalink
fix: minor fixes to measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLiegiBastonLiegi committed Jan 18, 2024
1 parent 49d47cf commit df05505
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/qibo/backends/clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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__)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/qibo/backends/clifford_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
5 changes: 4 additions & 1 deletion src/qibo/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit df05505

Please sign in to comment.