r/AskProgramming • u/Routine_Comb_7277 • Aug 09 '26
Python On the way to create my own Qiskit
Hi.I am on my way to create my own Qiskit. Here is the code
import numpy as np
class Circuit:
notGate = np.array([[0,1],[1,0]])
identityGate = np.array([[1,0],[0,1]])
hadamardGate = 1/np.sqrt(2)*np.array([[1,1],[1,-1]])
zetaGate = np.array([[1,0],[0,-1]])
listOfMatrices = []
def __init__(self):
self.initState = 1/np.sqrt(8)*np.array([[1],[1],[1],[1],[1],[1],[1],[1]])
def addNotControlledGate(self,gateName1,gateName2,gateName3):
g1 = gateName1
match g1:
case 'N':
gate1 = self.notGate
case 'I':
gate1 = self.identityGate
case 'H':
gate1 = self.hadamardGate
case 'Z':
gate1 = self.zetaGate
g2 = gateName2
match g2:
case 'N':
gate2 = self.notGate
case 'I':
gate2 = self.identityGate
case 'H':
gate2 = self.hadamardGate
case 'Z':
gate2 = self.zetaGate
g3 = gateName3
match g3:
case 'N':
gate3 = self.notGate
case 'I':
gate3 = self.identityGate
case 'H':
gate3 = self.hadamardGate
case 'Z':
gate3 = self.zetaGate
gate12 = np.kron(gate1,gate2)
gateOverall = np.kron(gate12,gate3)
print(gateOverall)
self.listOfMatrices.append(gateOverall)
return gateOverall
def addControlledGate(self,control,target,gateName):
g1 = gateName
match g1:
case 'N':
gate1 = self.notGate
case 'I':
gate1 = self.identityGate
case 'H':
gate1 = self.hadamardGate
case 'Z':
gate1 = self.zetaGate
zeroprefixgate = np.array([[1,0],[0,0]])
oneprefixgate = np.array([[0,0],[0,1]])
if control ==1:
if target == 2:
gate11 = np.kron(oneprefixgate,gate1)
gateOverall11 = np.kron(gate11,self.identityGate)
gate00 = np.kron(zeroprefixgate,self.identityGate)
gateOverall00 = np.kron(gate00,self.identityGate)
elif target == 3:
gate11 = np.kron(oneprefixgate,self.identityGate)
gateOverall11 = np.kron(gate11,gate1)
gate00 = np.kron(zeroprefixgate,self.identityGate)
gateOverall00 = np.kron(gate00,self.identityGate)
elif control ==2:
if target == 1:
gate11 = np.kron(gate1,oneprefixgate)
gateOverall11 = np.kron(gate11,self.identityGate)
gate00 = np.kron(self.identityGate,zeroprefixgate)
gateOverall00 = np.kron(gate00,self.identityGate)
elif target == 3:
gate11 = np.kron(self.identityGate,oneprefixgate)
gateOverall11 = np.kron(gate11, gate1)
gate00 = np.kron(self.identityGate,zeroprefixgate)
gateOverall00 = np.kron(gate00,self.identityGate)
elif control == 3:
if target == 1:
gate11 = np.kron(gate1,self.identityGate)
gateOverall11 = np.kron(gate11,oneprefixgate)
gate00 = np.kron(self.identityGate,self.identityGate)
gateOverall00 = np.kron(gate00,zeroprefixgate)
elif target == 2:
gate11 = np.kron(self.identityGate,gate1)
gateOverall11 = np.kron(gate11,oneprefixgate)
gate00 = np.kron(self.identityGate,self.identityGate)
gateOverall00 = np.kron(gate00,zeroprefixgate)
gateOverall = gateOverall00+gateOverall11
print(gateOverall)
self.listOfMatrices.append(gateOverall)
return gateOverall
def getState(self, i):
if i < 0:
return np.identity(8)
if i == 0:
return self.listOfMatrices[0]
x = self.listOfMatrices[i] @ self.getState(i - 1)
print(x)
return x
def getNormalisationFactorOfInitialState(self):
counter = 0
for i in range(len(self.initState)):
if self.initState[i] == 0:
continue
else:
counter+=1
print(counter)
return counter
def getFinalVector(self):
x = self.getState(1)
y = self.initState
z = x @ y
print(z)
return z
def measure(self):
x = self.getFinalVector()
y = np.abs(x)
z = np.square(y)
print(z)I want to now add a phase gate but im unsure on how to do it.My function addControlledGate and addNotControlledGate takes 3 arguments , but I want the user to be able to select what phase the phase gate will have.How to do it?Thx.