Skip to content

Commit

Permalink
replace asmatrix by matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Aug 10, 2023
1 parent 84c9053 commit dd7792c
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/qibo/backends/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ def plus_density_matrix(self, nqubits): # pragma: no cover
raise_error(NotImplementedError)

@abc.abstractmethod
def asmatrix(self, gate): # pragma: no cover
def matrix(self, gate): # pragma: no cover
"""Convert a :class:`qibo.gates.Gate` to the corresponding matrix."""
raise_error(NotImplementedError)

@abc.abstractmethod
def asmatrix_parametrized(self, gate): # pragma: no cover
"""Equivalent to :meth:`qibo.backends.abstract.Backend.asmatrix` for parametrized gates."""
def matrix_parametrized(self, gate): # pragma: no cover
"""Equivalent to :meth:`qibo.backends.abstract.Backend.matrix` for parametrized gates."""
raise_error(NotImplementedError)

@abc.abstractmethod
def asmatrix_fused(self, gate): # pragma: no cover
def matrix_fused(self, gate): # pragma: no cover
"""Fuse matrices of multiple gates."""
raise_error(NotImplementedError)

Expand Down
20 changes: 10 additions & 10 deletions src/qibo/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,25 @@ def plus_density_matrix(self, nqubits):
state /= 2**nqubits
return state

def asmatrix(self, gate):
def matrix(self, gate):
"""Convert a gate to its matrix representation in the computational basis."""
name = gate.__class__.__name__
matrix = getattr(self.matrices, name)
return matrix(2 ** len(gate.target_qubits)) if callable(matrix) else matrix
_matrix = getattr(self.matrices, name)
return _matrix(2 ** len(gate.target_qubits)) if callable(_matrix) else _matrix

def asmatrix_parametrized(self, gate):
def matrix_parametrized(self, gate):
"""Convert a parametrized gate to its matrix representation in the computational basis."""
name = gate.__class__.__name__
return getattr(self.matrices, name)(*gate.parameters)

def asmatrix_fused(self, fgate):
def matrix_fused(self, fgate):
rank = len(fgate.target_qubits)
matrix = np.eye(2**rank, dtype=self.dtype)
for gate in fgate.gates:
# transfer gate matrix to numpy as it is more efficient for
# small tensor calculations
# explicit to_numpy see https://github.com/qiboteam/qibo/issues/928
gmatrix = self.to_numpy(gate.asmatrix(self))
gmatrix = self.to_numpy(gate.matrix(self))
# Kronecker product with identity is needed to make the
# original matrix have shape (2**rank x 2**rank)
eye = np.eye(2 ** (rank - len(gate.qubits)), dtype=self.dtype)
Expand Down Expand Up @@ -147,7 +147,7 @@ def control_matrix(self, gate):
"unitary for more than two "
"control qubits.",
)
matrix = gate.asmatrix(self)
matrix = gate.matrix(self)
shape = matrix.shape
if shape != (2, 2):
raise_error(
Expand All @@ -163,7 +163,7 @@ def control_matrix(self, gate):
def apply_gate(self, gate, state, nqubits):
state = self.cast(state)
state = self.np.reshape(state, nqubits * (2,))
matrix = gate.asmatrix(self)
matrix = gate.matrix(self)
if gate.is_controlled_by:
matrix = self.np.reshape(matrix, 2 * len(gate.target_qubits) * (2,))
ncontrol = len(gate.control_qubits)
Expand All @@ -190,7 +190,7 @@ def apply_gate(self, gate, state, nqubits):
def apply_gate_density_matrix(self, gate, state, nqubits):
state = self.cast(state)
state = self.np.reshape(state, 2 * nqubits * (2,))
matrix = gate.asmatrix(self)
matrix = gate.matrix(self)
if gate.is_controlled_by:
matrix = self.np.reshape(matrix, 2 * len(gate.target_qubits) * (2,))
matrixc = self.np.conj(matrix)
Expand Down Expand Up @@ -239,7 +239,7 @@ def apply_gate_density_matrix(self, gate, state, nqubits):
def apply_gate_half_density_matrix(self, gate, state, nqubits):
state = self.cast(state)
state = np.reshape(state, 2 * nqubits * (2,))
matrix = gate.asmatrix(self)
matrix = gate.matrix(self)
if gate.is_controlled_by: # pragma: no cover
raise_error(
NotImplementedError,
Expand Down
12 changes: 6 additions & 6 deletions src/qibo/backends/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,16 @@ def zero_density_matrix(self, nqubits):
state = self.tf.tensor_scatter_nd_update(state, idx, update)
return state

def asmatrix(self, gate):
npmatrix = super().asmatrix(gate)
def matrix(self, gate):
npmatrix = super().matrix(gate)
return self.tf.cast(npmatrix, dtype=self.dtype)

def asmatrix_parametrized(self, gate):
npmatrix = super().asmatrix_parametrized(gate)
def matrix_parametrized(self, gate):
npmatrix = super().matrix_parametrized(gate)
return self.tf.cast(npmatrix, dtype=self.dtype)

def asmatrix_fused(self, gate):
npmatrix = super().asmatrix_fused(gate)
def matrix_fused(self, gate):
npmatrix = super().matrix_fused(gate)
return self.tf.cast(npmatrix, dtype=self.dtype)

def execute_circuit(
Expand Down
17 changes: 5 additions & 12 deletions src/qibo/gates/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def decompose(self, *free) -> List["Gate"]:
# of the same gate.
return [self.__class__(*self.init_args, **self.init_kwargs)]

def asmatrix(self, backend=None):
def matrix(self, backend=None):
"""Returns the matrix representation of the gate.
Args:
Expand All @@ -284,7 +284,7 @@ def asmatrix(self, backend=None):
if backend is None: # pragma: no cover
backend = GlobalBackend()

return backend.asmatrix(self)
return backend.matrix(self)

def generator_eigenvalue(self):
"""
Expand All @@ -306,13 +306,6 @@ def basis_rotation(self):
f"Basis rotation is not implemented for {self.__class__.__name__}",
)

@property
def matrix(self):
from qibo.backends import GlobalBackend

backend = GlobalBackend()
return self.asmatrix(backend)

def apply(self, backend, state, nqubits):
return backend.apply_gate(self, state, nqubits)

Expand All @@ -329,7 +322,7 @@ def commutes(self, gate):
def on_qubits(self, qubit_map):
raise_error(NotImplementedError, "Cannot use special gates on subroutines.")

def asmatrix(self, backend): # pragma: no cover
def matrix(self, backend=None): # pragma: no cover
raise_error(
NotImplementedError, "Special gates do not have matrix representation."
)
Expand Down Expand Up @@ -405,8 +398,8 @@ def substitute_symbols(self):
params[i] = float(param)
self.parameters = tuple(params)

def asmatrix(self, backend=None):
def matrix(self, backend=None):
if backend is None: # pragma: no cover
backend = GlobalBackend()

return backend.asmatrix_parametrized(self)
return backend.matrix_parametrized(self)
2 changes: 1 addition & 1 deletion src/qibo/gates/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def to_choi(self, nqubits: Optional[int] = None, order: str = "row", backend=Non
for coeff, gate in zip(self.coefficients, self.gates):
kraus_op = FusedGate(*range(nqubits))
kraus_op.append(gate)
kraus_op = kraus_op.asmatrix(backend)
kraus_op = kraus_op.matrix(backend)
kraus_op = vectorization(kraus_op, order=order, backend=backend)
super_op += coeff * np.outer(kraus_op, np.conj(kraus_op))
del kraus_op
Expand Down
2 changes: 1 addition & 1 deletion src/qibo/gates/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def controlled_by(self, *q):
""""""
raise_error(NotImplementedError, "Measurement gates cannot be controlled.")

def asmatrix(self, backend):
def matrix(self, backend=None):
""""""
raise_error(
NotImplementedError, "Measurement gates do not have matrix representation."
Expand Down
4 changes: 2 additions & 2 deletions src/qibo/gates/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def can_fuse(self, gate, max_qubits):
return False
return True

def asmatrix(self, backend=None):
def matrix(self, backend=None):
"""Returns matrix representation of special gate.
Args:
Expand All @@ -110,7 +110,7 @@ def asmatrix(self, backend=None):
if backend is None: # pragma: no cover
backend = GlobalBackend()

return backend.asmatrix_fused(self)
return backend.matrix_fused(self)

def fuse(self, gate):
"""Fuses two gates."""
Expand Down
2 changes: 1 addition & 1 deletion src/qibo/models/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ def unitary(self, backend=None):
for gate in self.queue:
if not isinstance(gate, (gates.SpecialGate, gates.M)):
fgate.append(gate)
return fgate.asmatrix(backend)
return fgate.matrix(backend)

@property
def final_state(self):
Expand Down
8 changes: 4 additions & 4 deletions src/qibo/quantum_info/superoperator_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def kraus_to_choi(kraus_ops, order: str = "row", backend=None):
for gate in gates:
kraus_op = FusedGate(*range(nqubits))
kraus_op.append(gate)
kraus_op = kraus_op.asmatrix(backend)
kraus_op = kraus_op.matrix(backend)
kraus_op = vectorization(kraus_op, order=order, backend=backend)
super_op += np.outer(kraus_op, np.conj(kraus_op))
del kraus_op
Expand Down Expand Up @@ -800,7 +800,7 @@ def kraus_to_chi(
for gate in gates:
kraus_op = FusedGate(*range(nqubits))
kraus_op.append(gate)
kraus_op = kraus_op.asmatrix(backend)
kraus_op = kraus_op.matrix(backend)
kraus_op = vectorization(kraus_op, order=order, backend=backend)
kraus_op = comp_to_pauli @ kraus_op
super_op += np.outer(kraus_op, np.conj(kraus_op))
Expand Down Expand Up @@ -884,7 +884,7 @@ def kraus_to_stinespring(
vector_alpha = backend.cast(vector_alpha, dtype=vector_alpha.dtype)
kraus_op = FusedGate(*range(nqubits))
kraus_op.append(gate)
kraus_op = kraus_op.asmatrix(backend)
kraus_op = kraus_op.matrix(backend)
kraus_op = backend.cast(kraus_op, dtype=kraus_op.dtype)
stinespring += np.kron(
kraus_op,
Expand Down Expand Up @@ -2223,7 +2223,7 @@ def _individual_kraus_to_liouville(
for gate in gates:
kraus_op = FusedGate(*range(nqubits))
kraus_op.append(gate)
kraus_op = kraus_op.asmatrix(backend)
kraus_op = kraus_op.matrix(backend)
kraus_op = vectorization(kraus_op, order=order, backend=backend)
kraus_op = np.outer(kraus_op, np.conj(kraus_op))
super_ops.append(choi_to_liouville(kraus_op, order=order, backend=backend))
Expand Down

0 comments on commit dd7792c

Please sign in to comment.