Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Commit

Permalink
docs for Clifford class and clifford_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ShellyGarion committed Feb 13, 2020
1 parent deffcdc commit 4069cc9
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 60 deletions.
44 changes: 22 additions & 22 deletions qiskit/ignis/verification/randomized_benchmarking/Clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

class Clifford:

"""Clifford class"""
"""Clifford Operator Class."""

def __init__(self, num_qubits=None, table=None, phases=None):
"""Initialize an n-qubit Clifford table."""
# Initialize an n-qubit Clifford table.
# Use index for initializing 1 and 2 qubit Cliffords
# If index is none initialize to identity.

Expand Down Expand Up @@ -107,11 +107,11 @@ def phases(self):
return self._phases

def __getitem__(self, index):
"""Get element from internal symplectic table"""
"""Get element from internal symplectic table."""
return self._table[index]

def __setitem__(self, index, value):
"""Set element of internal symplectic table"""
"""Set element of internal symplectic table."""
if isinstance(value, Pauli):
# Update from Pauli object
self._table[index] = np.block([value.z, value.x])
Expand All @@ -124,25 +124,25 @@ def __setitem__(self, index, value):
# ---------------------------------------------------------------------

def stabilizer(self, qubit):
"""Return the qubit stabilizer as a Pauli object"""
"""Return the qubit stabilizer as a Pauli object."""
nq = self._num_qubits
z = self._table[nq + qubit, 0:nq]
x = self._table[nq + qubit, nq:2 * nq]
return Pauli(z=z, x=x)

def update_stabilizer(self, qubit, pauli):
"""Update the qubit stabilizer row from a Pauli object"""
"""Update the qubit stabilizer row from a Pauli object."""
self[self._num_qubits + qubit] = pauli

def destabilizer(self, row):
"""Return the destabilizer as a Pauli object"""
"""Return the destabilizer as a Pauli object."""
nq = self._num_qubits
z = self._table[row, 0:nq]
x = self._table[row, nq:2 * nq]
return Pauli(z=z, x=x)

def update_destabilizer(self, qubit, pauli):
"""Update the qubit destabilizer row from a Pauli object"""
"""Update the qubit destabilizer row from a Pauli object."""
self[qubit] = pauli

# ---------------------------------------------------------------------
Expand All @@ -167,7 +167,7 @@ def as_dict(self):

@classmethod
def from_dict(cls, clifford_dict):
"""Load a Clifford from a dictionary"""
"""Load a Clifford from a dictionary."""

# Validation
if not isinstance(clifford_dict, dict) or \
Expand All @@ -185,7 +185,7 @@ def from_dict(cls, clifford_dict):

# Helper function
def get_row(label):
"""Return the Pauli object and phase for stabilizer"""
"""Return the Pauli object and phase for stabilizer."""
if label[0] in ['I', 'X', 'Y', 'Z']:
pauli = Pauli.from_label(label)
phase = 0
Expand Down Expand Up @@ -219,7 +219,7 @@ def index(self):
Returns a unique index for the Clifford.
Returns:
A unique index (integer).
A unique index (integer) for the Clifford object.
"""
mat = self.table
mat = mat.reshape(mat.size)
Expand All @@ -239,23 +239,23 @@ def index(self):
# NOTE: These might change based on changes to QuantumCircuit API.
# They should mimic the circuit API as much as possible.
def x(self, qubit):
"""Apply a Pauli "x" gate to a qubit"""
"""Apply a Pauli "x" gate to a qubit."""
iz = qubit
self._phases = np.logical_xor(self._phases, self._table[:, iz])

def y(self, qubit):
"""Apply an Pauli "y" gate to a qubit"""
"""Apply an Pauli "y" gate to a qubit."""
iz, ix = qubit, self._num_qubits + qubit
zx_xor = np.logical_xor(self._table[:, iz], self._table[:, ix])
self._phases = np.logical_xor(self._phases, zx_xor)

def z(self, qubit):
"""Apply an Pauli "z" gate to qubit"""
"""Apply an Pauli "z" gate to qubit."""
ix = self._num_qubits + qubit
self._phases = np.logical_xor(self._phases, self._table[:, ix])

def h(self, qubit):
"""Apply an Hadamard "h" gate to qubit"""
"""Apply an Hadamard "h" gate to qubit."""
iz, ix = qubit, self._num_qubits + qubit
zx_and = np.logical_and(self._table[:, ix], self._table[:, iz])
self._phases = np.logical_xor(self._phases, zx_and)
Expand All @@ -266,33 +266,33 @@ def h(self, qubit):
self._table[:, iz] = x_cache

def s(self, qubit):
"""Apply a phase "s" gate to qubit"""
"""Apply a phase "s" gate to qubit."""
iz, ix = qubit, self._num_qubits + qubit
zx_and = np.logical_and(self._table[:, ix], self._table[:, iz])
self._phases = np.logical_xor(self._phases, zx_and)
self._table[:, iz] = np.logical_xor(self._table[:, ix],
self._table[:, iz])

def sdg(self, qubit):
"""Apply an adjoint phase "sdg" gate to qubit"""
"""Apply an adjoint phase "sdg" gate to qubit."""
# TODO: change direct table update if more efficient
self.z(qubit)
self.s(qubit)

def v(self, qubit):
"""Apply v gate sd.h"""
"""Apply v gate v = sdg.h ."""
# TODO: change direct table update if more efficient
self.sdg(qubit)
self.h(qubit)

def w(self, qubit):
"""Apply w gate v.v"""
"""Apply w gate w = v.v ."""
# TODO: change direct table update if more efficient
self.h(qubit)
self.s(qubit)

def cx(self, qubit_ctrl, qubit_trgt):
"""Apply a Controlled-NOT "cx" gate"""
"""Apply a Controlled-NOT "cx" gate."""
# Helper indices for stabilizer columns
iz_c, ix_c = qubit_ctrl, self.num_qubits + qubit_ctrl
iz_t, ix_t = qubit_trgt, self.num_qubits + qubit_trgt
Expand All @@ -309,14 +309,14 @@ def cx(self, qubit_ctrl, qubit_trgt):
self._table[:, iz_c])

def cz(self, qubit_ctrl, qubit_trgt):
"""Apply a Controlled-z "cx" gate"""
"""Apply a Controlled-z "cz" gate."""
# TODO: change direct table update if more efficient
self.h(qubit_trgt)
self.cx(qubit_ctrl, qubit_trgt)
self.h(qubit_trgt)

def swap(self, qubit0, qubit1):
"""Apply SWAP gate between two qubits"""
"""Apply SWAP gate between two qubits."""
# TODO: change direct swap of required rows and cols in table
self.cx(qubit0, qubit1)
self.cx(qubit1, qubit0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def gatelist(self):
@abstractmethod
def load_tables(self):
"""Load pickled group tables,
or generate them if they do not exist"""
or generate them if they do not exist."""
return

@abstractmethod
Expand Down
Loading

0 comments on commit 4069cc9

Please sign in to comment.