from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, Aer, execute
from qiskit.extensions.simulator import snapshot
from qiskit.tools.visualization import circuit_drawer
import numpy as np
import math as m
import scipy as sci
import random
import time
import matplotlib
import matplotlib.pyplot as plt
S_simulator = Aer.backends(name='statevector_simulator')[0]
M_simulator = Aer.backends(name='qasm_simulator')[0]
#===============================================
#----------- Math Operations -----------
#===============================================
def Binary(N, total, LSB):
'''
Input: N (integer) total (integer) LSB (string)
Returns the base-2 binary equivilant of N according to left or right least significant bit notation
'''
qubits = int(m.log(total,2))
b_num = np.zeros(qubits)
for i in np.arange(qubits):
if( N/((2)**(qubits-i-1)) >= 1 ):
if(LSB=='R'):
b_num[i] = 1
if(LSB=='L'):
b_num[int(qubits-(i+1))] = 1
N = N - 2**(qubits-i-1)
B = []
for j in np.arange(len(b_num)):
B.append(int(b_num[j]))
return B
def QFT(qc, q, qubits, **kwargs):
'''
Input: qc (QuantumCircuit) q (QuantumRegister) qubits (integer)
Keyword Arguments: swap (Bool) - Adds SWAP gates after all of the phase gates have been applied
Assigns all the gate operations for a Quantum Fourier Transformation
'''
R_phis = [0]
for i in np.arange(2,int(qubits+1)):
R_phis.append( 2/(2**(i)) * m.pi )
for j in np.arange(int(qubits)):
qc.h( q[int(j)] )
for k in np.arange(int(qubits-(j+1))):
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
if 'swap' in kwargs:
if(kwargs['swap'] == True):
for s in np.arange(m.floor(qubits/2.0)):
qc.swap( q[int(s)],q[int(qubits-1-s)] )
#==================================================
#----------- Displaying Results -----------
#==================================================
def Wavefunction(obj, **kwargs):
'''
Prints a tidier version of the array statevector corresponding to the wavefuntion of a QuantumCircuit object
Keyword Arguments: precision (integer) - the decimal precision for amplitudes
column (Bool) - prints each state in a vertical column
systems (array of integers) - seperates the qubits into different states
show_systems (array of Bools) - indictates which qubit systems to print
'''
if(type(obj) == QuantumCircuit ):
statevec = execute( obj, S_simulator, shots=1 ).result().get_statevector()
if(type(obj) == np.ndarray):
statevec = obj
sys = False
NL = False
dec = 5
if 'precision' in kwargs:
dec = int( kwargs['precision'] )
if 'column' in kwargs:
NL = kwargs['column']
if 'systems' in kwargs:
systems = kwargs['systems']
sys = True
last_sys = int(len(systems)-1)
show_systems = []
for s_chk in np.arange(len(systems)):
if( type(systems[s_chk])!=int ):
raise Exception('systems must be an array of all integers')
if 'show_systems' in kwargs:
show_systems = kwargs['show_systems']
if( len(systems)!=len(show_systems) ):
raise Exception('systems and show_systems need to be arrays of equal length')
for ls in np.arange(len(show_systems)):
if((show_systems[ls]!=True)and(show_systems[ls]!=False)):
raise Exception('show_systems must be an array of Truth Values')
if(show_systems[ls]==True):
last_sys = int(ls)
else:
for ss in np.arange(len(systems)):
show_systems.append(True)
wavefunction = ''
qubits = int(m.log(len(statevec),2))
for i, comp in enumerate(statevec): #i in np.arange( int(len(statevec)) ):
value = round(comp.real, dec) + round(comp.imag, dec) * 1j
if( (value.real != 0) or (value.imag != 0) ):
state = list( Binary(int(i), int(2**qubits) ,'L') )
state_str = ''
if( sys == True ):
k = 0
for s in np.arange(len(systems)):
if(show_systems[s]==True):
if(int(s)!=last_sys):
state.insert( int(k+systems[s]),'>|' )
k = int(k+systems[s]+1)
else:
k = int(k+systems[s])
else:
for s2 in np.arange(systems[s]):
del state[int(k)]
for j in np.arange(len(state)):
if(type(state[j])!=str):
state_str = state_str+str(int(state[j]))
else:
state_str = state_str+state[j]
if( (value.real != 0) and (value.imag != 0) ):
if( value.imag > 0):
wavefunction = wavefunction + str(value.real) + '+' + str(value.imag) + 'j |' + state_str + '> '
else:
wavefunction = wavefunction +str(value.real)+''+str(value.imag)+'j |'+state_str+'> '
if( (value.real!=0) and (value.imag==0) ):
wavefunction = wavefunction + str(value.real) + ' |' + state_str + '> '
if( (value.real==0) and (value.imag!=0) ):
wavefunction = wavefunction +str(value.imag)+'j |'+state_str+'> '
if(NL):
wavefunction = wavefunction + '\n'
print(wavefunction)
def Wavefunction_orig_modified(obj, **kwargs):
'''
Prints a tidier version of the array statevector corresponding to the wavefuntion of a QuantumCircuit object
Keyword Arguments: precision (integer) - the decimal precision for amplitudes
column (Bool) - prints each state in a vertical column
systems (array of integers) - seperates the qubits into different states
show_systems (array of Bools) - indictates which qubit systems to print
'''
if(type(obj) == QuantumCircuit ):
statevec = execute( obj, S_simulator, shots=1 ).result().get_statevector()
if(type(obj) == np.ndarray):
statevec = obj
sys = False
NL = False
dec = 5
if 'precision' in kwargs:
dec = int( kwargs['precision'] )
if 'column' in kwargs:
NL = kwargs['column']
if 'systems' in kwargs:
systems = kwargs['systems']
sys = True
last_sys = int(len(systems)-1)
show_systems = []
for s_chk in np.arange(len(systems)):
if( type(systems[s_chk])!=int ):
raise Exception('systems must be an array of all integers')
if 'show_systems' in kwargs:
show_systems = kwargs['show_systems']
if( len(systems)!=len(show_systems) ):
raise Exception('systems and show_systems need to be arrays of equal length')
for ls in np.arange(len(show_systems)):
if((show_systems[ls]!=True)and(show_systems[ls]!=False)):
raise Exception('show_systems must be an array of Truth Values')
if(show_systems[ls]==True):
last_sys = int(ls)
else:
for ss in np.arange(len(systems)):
show_systems.append(True)
wavefunction = ''
qubits = int(m.log(len(statevec),2))
for i, comp in enumerate(statevec): #i in np.arange( int(len(statevec)) ):
value = round(comp.real, dec) + round(comp.imag, dec) * 1j
if( (value.real != 0) or (value.imag != 0) ):
state = list(Binary(int(i), int(2**qubits) ,'L') )
state_str = ''
if( sys == True ):
k = 0
for s in np.arange(len(systems)):
if(show_systems[s]==True):
if(int(s)!=last_sys):
state.insert( int(k+systems[s]),'>|' )
k = int(k+systems[s]+1)
else:
k = int(k+systems[s])
else:
for s2 in np.arange(systems[s]):
del state[int(k)]
for j in np.arange(len(state)):
if(type(state[j])!=str):
state_str = state_str+str(int(state[j]))
else:
state_str = state_str+state[j]
if( (value.real != 0) and (value.imag != 0) ):
if( value.imag > 0):
wavefunction = wavefunction + str(value.real) + '+' + str(value.imag) + 'j |' + state_str + '> '
else:
wavefunction = wavefunction +str(value.real)+''+str(value.imag)+'j |'+state_str+'> '
if( (value.real!=0) and (value.imag==0) ):
wavefunction = wavefunction + str(value.real) + ' |' + state_str + '>'
if( (value.real==0) and (value.imag!=0) ):
wavefunction = wavefunction +str(value.imag)+'j |'+state_str+'> '
if(NL):
wavefunction = wavefunction + '\n'
print(wavefunction)
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, Aer, execute
import numpy as np
import math as m
import scipy as sci
S_simulator = Aer.backends(name='statevector_simulator')[0]
M_simulator = Aer.backends(name='qasm_simulator')[0]
#Displaying Results
def Wavefunction_new( obj , *args, **kwargs):
#Displays the waveftmction of the quantum system
if(type(obj) == QuantumCircuit ):
statevec = execute( obj, S_simulator, shots=1 ).result().get_statevector()
if(type(obj) == np.ndarray):
statevec = obj
sys = False
NL = False
dec = 5
if 'precision' in kwargs:
dec = int( kwargs['precision'] )
if 'column' in kwargs:
NL = kwargs['column']
if 'systems' in kwargs:
systems = kwargs['systems']
sys = True
last_sys = int(len(systems)-1)
show_systems = []
for s_chk in np.arange(len(systems)):
if( type(systems[s_chk]) != int ):
raise Exception('systems must be an array of all integers')
if 'show_systems' in kwargs:
show_systems = kwargs['show_systems']
if( len(systems)!= len(show_systems) ):
raise Exception('systems and show_systems need to be arrays of equal length')
for ls in np.arange(len(show_systems)):
if((show_systems[ls] != True) and (show_systems[ls] != False)):
raise Exception('show_systems must be an array of Truth Values')
if(show_systems[ls] == True):
last_sys = int(ls)
else:
for ss in np.arange(len(systems)):
show_systems.append(True)
wavefunction = ''
qubits = int(m.log(len(statevec),2))
for i, comp in enumerate(statevec): #i in np.arange( int(len(statevec)) ):
value = round(comp.real, dec) + round(comp.imag, dec) * 1j
# for i in np.arange( int(len(statevec))):
#print(wavefunction)
# value = round(statevec[i].real, dec) + round(statevec[i].imag, dec) * 1j
if( (value.real != 0) or (value.imag != 0)):
state = list(Binary(int(i),int(2**qubits)))
state.reverse()
state_str = ''
#print(state)
if( sys == True ): #Systems and SharSystems
k = 0
for s in np.arange(len(systems)):
if(show_systems[s] == True):
if(int(s) != last_sys):
state.insert(int(k + systems[s]), '>|' )
k = int(k + systems[s] + 1)
else:
k = int(k + systems[s])
else:
for s2 in np.arange(systems[s]):
del state[int(k)]
for j in np.arange(len(state)):
if(type(state[j])!= str):
state_str = state_str + str(int(state[j]))
else:
state_str = state_str + state[j]
#print(state_str)
#print(value)
if( (value.real != 0) and (value.imag != 0) ):
if( value.imag > 0):
wavefunction = wavefunction + str(value.real) + '+' + str(value.imag) + 'j |' + state_str + '> '
else:
wavefunction = wavefunction + str(value.real) + '' + str(value.imag) + 'j |' + state_str + '> '
if( (value.real !=0 ) and (value.imag ==0) ):
wavefunction = wavefunction + str(value.real) + ' |' + state_str + '> '
if( (value.real == 0) and (value.imag != 0) ):
wavefunction = wavefunction + str(value.imag) + 'j |' + state_str + '> '
if(NL):
wavefunction = wavefunction + '\n'
#print(NL)
print(wavefunction)
#return wavefunction
def QFT(qc, q, qubits, **kwargs):
'''
Input: qc (QuantumCircuit) q (QuantumRegister) qubits (integer)
Keyword Arguments: swap (Bool) - Adds SWAP gates after all of the phase gates have been applied
Assigns all the gate operations for a Quantum Fourier Transformation
'''
R_phis = [0]
for i in np.arange(2,int(qubits+1)):
R_phis.append( 2/(2**(i)) * m.pi )
for j in np.arange(int(qubits)):
qc.h( q[int(j)] )
for k in np.arange(int(qubits-(j+1))):
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
if 'swap' in kwargs:
if(kwargs['swap'] == True):
for s in np.arange(m.floor(qubits/2.0)):
qc.swap( q[int(s)],q[int(qubits-1-s)] )
def QFT_dgr(qc, q, qubits, **kwargs):
'''
Input: qc (QuantumCircuit) q (QuantumRegister) qubits (integer)
Keyword Arguments: swap (Bool) - Adds SWAP gates after all of the phase gates have been applied
Assigns all the gate operations for a Quantum Fourier Transformation
'''
if 'swap' in kwargs:
if(kwargs['swap'] == True):
for s in np.arange(m.floor(qubits/2.0)):
qc.swap( q[int(s)],q[int(qubits-1-s)] )
R_phis = [0]
for i in np.arange(2,int(qubits+1)):
R_phis.append( -2/(2**(i)) * m.pi )
for j in np.arange(int(qubits)):
for k in np.arange(int(j)):
qc.cu1(R_phis[int(j-k)], q[int(qubits-(k+1))], q[int(qubits-(j+1))] )
qc.h( q[int(qubits-(j+1))] )
def DFT(x, **kwargs):
'''
Input: x (array)
Keyword Arguments: inverse (Bool) - if True, performs a Inverse Discrete Fourier Transformation instead
Computes a classical Discrete Fourier Transformation on the array of values x, returning a new array of transformed val
'''
p = -1.0
if 'inverse' in kwargs:
P = kwargs['inverse']
if(P == True):
p = 1.0
L = len(x)
X = []
for i in np.arange(L):
value = 0
for j in np.arange(L):
value = value + x[j]*np.exp(p*2*m.pi*1.0j * ( int(i*j)/(L*1.0) ) )
X.append(value)
for k in np.arange(len(X)):
re = round(X[k].real,5)
im = round(X[k].imag,5)
if( (abs(im) == 0) and (abs(re) != 0) ):
X[k] = re
elif( (abs(re) == 0) and (abs(im) != 0) ):
X[k] = im*1.0j
elif( (abs(re) == 0) and (abs(im) == 0) ):
X[k] = 0
else:
X[k] = re + im*1.0j
return X
#=================================================
#----------- Custom Operations -----------
#=================================================
def X_Transformation(qc, qreg, state):
'''
Input: qc (QuantumCircuit) qreg (QuantumRegister) state (array)
Applies the neccessary X gates to transform 'state' to the state of all 1's
'''
for j in np.arange(len(state)):
if( int(state[j])==0 ):
qc.x( qreg[int(j)] )
#==========================================================
#----------- Lesson 6.1 - Quantum Adder ------------
#==========================================================
def Quantum_Adder(qc, Qa, Qb, A, B):
'''
Input: qc (QuantumCircuit) Qa (QuantumRegister) Qb (QuantumRegister) A (array) B (array)
Appends all of the gate operations for a QFT based addition of two states A and B
'''
Q = len(B)
for n in np.arange(Q):
if( A[n] == 1 ):
qc.x( Qa[int(n+1)] )
if( B[n] == 1 ):
qc.x( Qb[int(n)] )
QFT(qc,Qa,Q+1)
p = 1
for j in np.arange( Q ):
qc.cu1( m.pi/(2**p), Qb[int(j)], Qa[0] )
p = p + 1
for i in np.arange(1,Q+1):
p = 0
for jj in np.arange( i-1, Q ):
qc.cu1( m.pi/(2**p), Qb[int(jj)], Qa[int(i)] )
p = p + 1
QFT_dgr(qc,Qa,Q+1)
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, Aer, execute
S_simulator = Aer.backends(name = 'statevector_simulator')[0]
q = QuantumRegister(1)
hello_qubit = QuantumCircuit(q)
hello_qubit.id(q[0])
job = execute(hello_qubit, S_simulator)
print(type(job))
result = job.result()
print(type(result))
statevector = result.get_statevector()
print(type(statevector))
q = QuantumRegister(3)
three_qubits = QuantumCircuit(q)
three_qubits.id(q[0])
three_qubits.id(q[1])
three_qubits.id(q[2])
job = execute(three_qubits, S_simulator)
result = job.result()
result.get_statevector()
|000⟩ , |100⟩ , |010⟩ , |110⟩ , |001⟩ , |101⟩ , |011⟩ , |111⟩
The state |100⟩ is represented when the qubit 0 is in |1⟩:
q = QuantumRegister(3)
three_qubits = QuantumCircuit(q)
three_qubits.x(q[0])
three_qubits.id(q[1])
three_qubits.id(q[2])
job = execute(three_qubits, S_simulator)
result = job.result()
result.get_statevector()
q = QuantumRegister(3)
three_qubits = QuantumCircuit(q)
three_qubits.x(q[0])
three_qubits.id(q[1])
three_qubits.id(q[2])
Wavefunction(three_qubits)
q = QuantumRegister(1)
H_circuit = QuantumCircuit(q)
H_circuit.h(q[0])
Wavefunction(H_circuit)
q = QuantumRegister(2)
H_circuit = QuantumCircuit(q)
H_circuit.h(q[0])
H_circuit.h(q[1])
Wavefunction(H_circuit)
from qiskit import ClassicalRegister
M_simulator = Aer.backends(name = 'qasm_simulator')[0]
q = QuantumRegister(1)
c = ClassicalRegister(1)
qc = QuantumCircuit(q,c)
qc.h(q[0])
qc.measure(q,c)
job = execute(qc, M_simulator)
result = job.result()
result.get_counts(qc)
q = QuantumRegister(2)
c = ClassicalRegister(2)
qc = QuantumCircuit(q,c)
qc.h(q[0])
qc.h(q[1])
qc.measure(q,c)
M = execute(qc, M_simulator).result().get_counts(qc)
print(M)
q = QuantumRegister(2)
c = ClassicalRegister(2)
qc = QuantumCircuit(q,c)
qc.h(q[0])
qc.h(q[1])
qc.measure(q[0],c[0])
M = execute(qc, M_simulator).result().get_counts(qc)
print(M)
q = QuantumRegister(2)
c = ClassicalRegister(2)
qc = QuantumCircuit(q,c)
qc.h(q[0])
qc.h(q[1])
qc.measure(q[0],c[0])
M = execute(qc, M_simulator).result().get_counts(qc)
print(M)
## Controlled Phase Gate - cu1
q = QuantumRegister(2, name = "q")
cu1_qc = QuantumCircuit(q, name = "qc")
cu1_qc.x(q[0])
cu1_qc.h(q[1])
print("__ Initial __" )
Wavefunction(cu1_qc)
cu1_qc.cu1(m.pi/4, q[0], q[1])
print("__ Final __" )
Wavefunction(cu1_qc)
circuit_drawer(cu1_qc)
q = QuantumRegister(2, name = "q")
swap_qc = QuantumCircuit(q, name = "qc")
swap_qc.x(q[0])
swap_qc.h(q[1])
print("__ Initial __" )
Wavefunction(swap_qc)
swap_qc.swap(q[0], q[1])
print("__ Final __" )
Wavefunction(swap_qc)
circuit_drawer(swap_qc)
import numpy as np
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, transpile, Aer, IBMQ
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from ibm_quantum_widgets import *
from qiskit.providers.aer import QasmSimulator
# Loading your IBM Quantum account(s)
provider = IBMQ.load_account()
from qiskit import ClassicalRegister, QuantumRegister, execute
import math as m
import numpy as np
q = QuantumRegister(4,name='q')
qc = QuantumCircuit(q,name='qc')
#-------------------------------
qc.x( q[0] )
qc.x( q[2] )
print('__ Initial State __')
Wavefunction(qc)
qc.h( q[0] )
qc.x( q[1] )
qc.y( q[2] )
qc.z( q[3] )
print('\n__ Operator: H + X + Y + Z __')
Wavefunction(qc)
qc.h( q[0] )
qc.x( q[1] )
qc.y( q[2] )
qc.z( q[3] )
print('\n__ Operator: H + X + Y + Z __')
Wavefunction(qc)
q = QuantumRegister(1,name='q')
qc = QuantumCircuit(q,name='qc')
#----------------------------------
print('__ Initial State __')
Wavefunction(qc)
qc.x( q[0] )
qc.z( q[0] )
print('\n__ Opertor: XZ __')
Wavefunction(qc)
qc.x( q[0] )
qc.z( q[0] )
print('\n__ Opertor: XZ __')
Wavefunction(qc)
q = QuantumRegister(1,name='q')
qc = QuantumCircuit(q,name='qc')
#-------------------------------
print('__ Initial State __')
Wavefunction(qc)
qc.x( q[0] )
qc.z( q[0] )
print('\n__ Opertor: XZ __')
Wavefunction(qc)
qc.z( q[0] )
qc.x( q[0] )
print('\n__ Opertor: ZX __')
Wavefunction(qc)
q = QuantumRegister(2,name='q')
qc = QuantumCircuit(q,name='qc')
#-------------------------------
qc.x( q[0] )
qc.h( q[0] )
qc.x( q[1] )
qc.h( q[1] )
print('__ Initial State __')
Wavefunction(qc)
qc.h( q[0] )
qc.cu1( m.pi/2, q[1], q[0] )
qc.h( q[1] )
print('\n__ After QFT __')
Wavefunction(qc)
q = QuantumRegister(3,name='q')
qc = QuantumCircuit(q,name='qc')
#---------------------------------
qc.x( q[2] )
print('__ Initial State __')
Wavefunction(qc)
#------------------------- qubit 0
qc.h( q[0] )
qc.cu1( m.pi/2,q[1],q[0] )
qc.cu1( m.pi/4,q[2],q[0] )
#------------------------- qubit 1
qc.h( q[1] )
qc.cu1( m.pi/2,q[2],q[1] )
#------------------------- qubit 2
qc.h( q[2] )
print('\n__ After QFT __')
Wavefunction(qc)
q = QuantumRegister(3,name='q')
qc = QuantumCircuit(q,name='qc')
#-------------------------------
qc.x( q[2] )
print('__ Initial State __')
Wavefunction(qc)
QFT(qc,q,3)
print('\n__ After QFT __')
Wavefunction(qc)
q = QuantumRegister(2,name='q')
qc = QuantumCircuit(q,name='qc')
#-------------------------------
qc.x( q[0] )
qc.h( q[0] )
qc.x( q[1] )
qc.h( q[1] )
print('__ Initial State __')
Wavefunction(qc)
QFT(qc,q,2)
print('\n__ First QFT __')
Wavefunction(qc)
QFT(qc,q,2)
print('\n__ Second QFT __')
Wavefunction(qc)
q = QuantumRegister(2,name='q')
qc = QuantumCircuit(q,name='qc')
#-------------------------------
qc.x( q[0] )
qc.h( q[0] )
qc.x( q[1] )
qc.h( q[1] )
print('__ Initial State __')
Wavefunction(qc)
qc.h( q[0] )
qc.cu1( m.pi/2,q[1],q[0] )
qc.h( q[1] )
print('\n__ QFT __')
Wavefunction(qc)
qc.h( q[1] )
qc.cu1( -m.pi/2,q[1],q[0] )
qc.h( q[0] )
print('\n__ Inverse QFT __')
Wavefunction(qc)
q = QuantumRegister(2,name='q')
qc = QuantumCircuit(q,name='qc')
#---------------------------------
qc.x( q[0] )
qc.h( q[0] )
qc.x( q[1] )
qc.h( q[1] )
print('__ Initial State __')
Wavefunction(qc)
QFT(qc,q,2, swap=True)
print('\n__ QFT __')
Wavefunction(qc)
QFT_dgr(qc,q,2, swap=True)
print('\n__ Inverse QFT __')
Wavefunction(qc)
X = [0,1/m.sqrt(8),0,0,0,0,0,0]
FX = DFT( X, inverse=True)
print('_____ DFT\u2020 _____')
for i in np.arange(len(FX)):
print('State: ',Binary(int(i),2**3,'R'),' Amplitude: ',FX[i])
#=======================================================================
q = QuantumRegister(3,name='q')
qc = QuantumCircuit(q,name='qc')
qc.x( q[2] )
print('\n Initial WF')
Wavefunction(qc)
QFT(qc,q,3)
print('\n_____ QFT _____')
Wavefunction(qc)
X = [0,1/m.sqrt(8),0,0,0,0,0,0]
FX = DFT( X, inverse=True)
print('_____ DFT\u2020 _____')
for i in np.arange(len(FX)):
print('State: ', Binary(int(i),2**3,'R'),' Amplitude: ',FX[i])
#=======================================================================
q = QuantumRegister(3,name='q')
qc = QuantumCircuit(q,name='qc')
qc.x( q[2] )
QFT(qc,q,3)
qc.swap(q[0],q[2])
print('\n_____ QFT + SWAP _____')
Wavefunction(qc)
marked = [1,0]
print('marked state: ',marked)
q = QuantumRegister(2,name='q')
anc = QuantumRegister(1,name='anc')
qc = QuantumCircuit(q,anc,name='qc')
#------------------------------------
qc.x( anc[0] )
QFT(qc,q,2)
print('\n____ Initial State (QFT) ____')
Wavefunction(qc, systems=[2,1], show_systems=[True,False])
X_Transformation(qc, q, marked)
qc.h( anc[0] )
qc.ccx( q[0], q[1], anc[0] )
X_Transformation(qc, q, marked)
qc.h( anc[0] )
print('\n____ Flip the Marked State ____')
Wavefunction(qc, systems=[2,1], show_systems=[True,False])
QFT(qc,q,2)
print('\n____ QFT ____')
Wavefunction(qc, systems=[2,1], show_systems=[True,False])
qc.h( anc[0] )
X_Transformation(qc, q, [0,0])
qc.ccx( q[0], q[1], anc[0] )
qc.h( anc[0] )
X_Transformation(qc, q, [0,0])
print('\n____ Flip the |00> state ____')
Wavefunction(qc, systems=[2,1], show_systems=[True,False])
QFT_dgr(qc,q,2)
print('\n____ QFT\u2020 ____')
Wavefunction(qc, systems=[2,1], show_systems=[True,False])
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, Aer, execute
import numpy as np
import math as m
q = QuantumRegister(3,name='q')
qc= QuantumCircuit(q,name='qc')
#------------------------------
qc.h( q[0] )
qc.h( q[1] )
qc.h( q[2] )
qc.u1( 3*m.pi/2, q[0] )
qc.u1( m.pi, q[1] )
qc.u1( 0, q[2] )
Wavefunction(qc)
q = QuantumRegister(3,name='q')
qc= QuantumCircuit(q,name='qc')
#------------------------------
qc.x( q[0] )
qc.x( q[1] )
QFT(qc,q,3)
Wavefunction(qc)
qa = QuantumRegister(2,name='a')
qb = QuantumRegister(2,name='b')
qc = QuantumCircuit(qa,qb,name='qc')
#------------------------------------
qc.x( qa[1] )
qc.x( qb[0] )
print('_____ States to Add Together _____')
Wavefunction(qc,systems=[2,2])
QFT(qc,qa,2)
#----------------------------- phase contributions from |b>
qc.cu1( m.pi, qb[0], qa[0] )
qc.cu1( m.pi/2, qb[1], qa[0] )
qc.cu1( m.pi, qb[1], qa[1] )
#-----------------------------
QFT_dgr(qc,qa,2)
print('\n___ Sum Stored in |a> ___')
Wavefunction(qc,systems=[2,2],show_systems=[True,False])
A_States = [[0,1],[1,0],[1,1]]
B_States = [[1,0],[1,1]]
#------------------------------------
for a in np.arange(len(A_States)):
A = A_States[a]
for b in np.arange(len(B_States)):
B = B_States[b]
qa = QuantumRegister(2,name='a')
qb = QuantumRegister(2,name='b')
qc = QuantumCircuit(qa,qb,name='qc')
#-----------------------------------
if(A[0]==1):
qc.x( qa[0] )
if(A[1]==1):
qc.x( qa[1] )
if(B[0]==1):
qc.x( qb[0] )
if(B[1]==1):
qc.x( qb[1] )
QFT(qc,qa,2)
qc.cu1( m.pi, qb[0], qa[0] )
qc.cu1( m.pi/2, qb[1], qa[0] )
qc.cu1( m.pi, qb[1], qa[1] )
QFT_dgr(qc,qa,2)
print('\nA:',A,' B:',B)
Wavefunction(qc,systems=[2,2],show_systems=[True,False])
qa = QuantumRegister(3,name='a')
qb = QuantumRegister(2,name='b')
qc = QuantumCircuit(qa,qb,name='qc')
#-----------------------------------
qc.x( qa[1] )
qc.x( qb[0] )
qc.x( qb[1] )
print('_____ States to Add Together _____')
Wavefunction(qc,systems=[3,2])
QFT(qc,qa,3)
#------------------------------ phase contributions from |b>
qc.cu1( m.pi/2, qb[0], qa[0] )
qc.cu1( m.pi/4, qb[1], qa[0] )
qc.cu1( m.pi, qb[0], qa[1] )
qc.cu1( m.pi/2, qb[1], qa[1] )
qc.cu1( m.pi, qb[1], qa[2] )
#------------------------------
QFT_dgr(qc,qa,3)
print('\n___ Sum Stored in |a> ___')
Wavefunction(qc,systems=[3,2],show_systems=[True,False])
A = [0,1,1,0]
B = [1,1,0,1]
#A and B need to be arrays of equal length (don't include the extra 0 qubit for A)
print('States to Add Together: ',A,' + ',B)
#=========================================
qa = QuantumRegister(len(A)+1,name='a')
qb = QuantumRegister(len(B),name='b')
qc = QuantumCircuit(qa,qb,name='qc')
#--------------------------------------
Quantum_Adder(qc,qa,qb,A,B)
print('\n___ Sum Stored in |a> ___')
Wavefunction(qc,systems=[len(A)+1,len(B)],show_systems=[True,False])
qa = QuantumRegister(3,name='a')
qb = QuantumRegister(3,name='b')
qc = QuantumCircuit(qa,qb,name='qc')
#-----------------------------------
qc.x( qa[0] )
qc.x( qa[1] )
qc.x( qb[1] )
qc.x( qb[2] )
print('____ States to Subtract ____')
Wavefunction(qc,systems=[3,3])
QFT(qc,qa,3)
#------------------------------ phase contributions from |b>
qc.cu1( -m.pi, qb[0], qa[0] )
qc.cu1( -m.pi/2, qb[1], qa[0] )
qc.cu1( -m.pi/4, qb[2], qa[0] )
qc.cu1( -m.pi, qb[1], qa[1] )
qc.cu1( -m.pi/2, qb[2], qa[1] )
qc.cu1( -m.pi, qb[2], qa[2] )
#------------------------------
QFT_dgr(qc,qa,3)
print('\n___ Difference Stored in |a> ___')
Wavefunction(qc,systems=[3,3],show_systems=[True,False])