Skip to content

Commit

Permalink
Create qgate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jun 2, 2024
1 parent 022872f commit ca83cb4
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/cosmic_pi_network/qclib/qgate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import numpy as np

class QGate:
def __init__(self, num_qubits):
self.num_qubits = num_qubits
self.matrix = np.eye(2**num_qubits, dtype=complex)

def __mul__(self, other):
if isinstance(other, QGate):
return QGate(self.num_qubits, np.dot(self.matrix, other.matrix))
elif isinstance(other, np.ndarray):
return np.dot(self.matrix, other)
else:
raise TypeError("Unsupported operand type for *: '{}' and '{}'".format(type(self).__name__, type(other).__name__))

def __str__(self):
return str(self.matrix)

def __repr__(self):
return repr(self.matrix)

@staticmethod
def pauli_x():
return QGate(1, np.array([[0, 1], [1, 0]]))

@staticmethod
def pauli_y():
return QGate(1, np.array([[0, -1j], [1j, 0]]))

@staticmethod
def pauli_z():
return QGate(1, np.array([[1, 0], [0, -1]]))

@staticmethod
def hadamard():
return QGate(1, 1/np.sqrt(2) * np.array([[1, 1], [1, -1]]))

@staticmethod
def cnot(control, target):
matrix = np.eye(2**2, dtype=complex)
matrix[2, 2] = 0
matrix[2, 3] = 1
matrix[3, 2] = 1
matrix[3, 3] = 0
return QGate(2, matrix)

@staticmethod
def toffoli(control1, control2, target):
matrix = np.eye(2**3, dtype=complex)
matrix[6, 6] = 0
matrix[6, 7] = 1
matrix[7, 6] = 1
matrix[7, 7] = 0
return QGate(3, matrix)

@staticmethod
def fredkin(control, target1, target2):
matrix = np.eye(2**3, dtype=complex)
matrix[4, 4] = 0
matrix[4, 5] = 1
matrix[5, 4] = 1
matrix[5, 5] = 0
matrix[6, 6] = 0
matrix[6, 7] = 1
matrix[7, 6] = 1
matrix[7, 7] = 0
return QGate(3, matrix)

def apply(self, state):
return np.dot(self.matrix, state)

def dagger(self):
return QGate(self.num_qubits, np.conj(self.matrix).T)

0 comments on commit ca83cb4

Please sign in to comment.