Library¶

In [7]:
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)

In [14]:
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))

<class 'qiskit.providers.aer.jobs.aerjob.AerJob'>
<class 'qiskit.result.result.Result'>
<class 'qiskit.providers.aer.backends.compatibility.Statevector'>


Registers, Gates and Cirquits ¶

In [15]:
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()

Statevector([1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j],
dims=(2, 2, 2))


|000⟩ , |100⟩ , |010⟩ , |110⟩ , |001⟩ , |101⟩ , |011⟩ , |111⟩

The state |100⟩ is represented when the qubit 0 is in |1⟩:

In [20]:
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()

Statevector([0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j,
0.+0.j],
dims=(2, 2, 2))

In [21]:
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)

1.0 |100>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):

In [22]:
q = QuantumRegister(1)
H_circuit = QuantumCircuit(q)

H_circuit.h(q[0])
Wavefunction(H_circuit)

0.70711 |0>  0.70711 |1>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):

In [19]:
q = QuantumRegister(2)
H_circuit = QuantumCircuit(q)

H_circuit.h(q[0])
H_circuit.h(q[1])
Wavefunction(H_circuit)

0.5 |00>  0.5 |10>  0.5 |01>  0.5 |11>

/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):


Measurement¶

In [23]:
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)

Out[23]:
{'1': 505, '0': 519}
In [24]:
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)

{'01': 245, '10': 282, '00': 253, '11': 244}

In [23]:
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)

{'00': 568, '01': 456}

In [ ]:
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)


Gates - Controlled Phase and Swap¶

In [25]:
## 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)

__ Initial __
0.70711 |10>  0.70711 |11>
__ Final __
0.70711 |10>  0.5+0.5j |11>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_79/3701507966.py:11: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
cu1_qc.cu1(m.pi/4, q[0], q[1])

Out[25]:

SWAP¶

In [26]:
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)

__ Initial __
0.70711 |10>  0.70711 |11>
__ Final __
0.70711 |01>  0.70711 |11>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):

Out[26]:

Quantum Fourier Transform¶

In [27]:
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


ibmqfactory.load_account:WARNING:2022-06-24 14:17:28,783: Credentials are already in use. The existing account in the session will be replaced.


Hemitian^2¶

In [2]:
from qiskit import ClassicalRegister, QuantumRegister, execute
import math as m
import numpy as np


In [4]:
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)

__ Initial State __
1.0 |1010>

__ Opertor: H + X + Y + Z __
-0.70711j |0100> 0.70711j |1100>

__ Opertor: H + X + Y + Z __
1.0 |1010>

/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):

In [28]:
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)

__ Initial State __
1.0 |0>

__ Opertor: XZ __
-1.0 |1>

__ Opertor: XZ __
-1.0 |0>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):

In [29]:
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)

__ Initial State __
1.0 |0>

__ Opertor: XZ __
-1.0 |1>

__ Opertor: ZX __
1.0 |0>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):


QFT¶

In [30]:
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)

__ Initial State __
0.5 |00>  -0.5 |10>  -0.5 |01>  0.5 |11>

__ After QFT __
0.5-0.5j |10>  0.5+0.5j |11>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_79/3125796127.py:11: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/2, q[1], q[0] )


In [32]:
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)

__ Initial State __
1.0 |001>

__ After QFT __
0.35355 |000>  0.25+0.25j |100>  0.35355j |010> -0.25+0.25j |110>  -0.35355 |001>  -0.25-0.25j |101>  -0.35355j |011> 0.25-0.25j |111>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_79/1260550585.py:9: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/2,q[1],q[0] )
/tmp/ipykernel_79/1260550585.py:10: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/4,q[2],q[0] )
/tmp/ipykernel_79/1260550585.py:13: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/2,q[2],q[1] )

In [9]:
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)

__ Initial State __
1.0 |001>

__ After QFT __
0.35355 |000>  0.25+0.25j |100>  0.35355j |010> -0.25+0.25j |110>  -0.35355 |001>  -0.25-0.25j |101>  -0.35355j |011> 0.25-0.25j |111>

/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_59/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )

In [10]:
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)

__ Initial State __
0.5 |00>  -0.5 |10>  -0.5 |01>  0.5 |11>

__ First QFT __
0.5-0.5j |10>  0.5+0.5j |11>

__ Second QFT __
0.5 |00>  -0.5j |01> -0.5+0.5j |11>

/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_59/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )

In [11]:
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)

__ Initial State __
0.5 |00>  -0.5 |10>  -0.5 |01>  0.5 |11>

__ QFT __
0.5-0.5j |10>  0.5+0.5j |11>

__ Inverse QFT __
0.5 |00>  -0.5 |10>  -0.5 |01>  0.5 |11>

/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_59/3665750724.py:11: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/2,q[1],q[0] )
/tmp/ipykernel_59/3665750724.py:16: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( -m.pi/2,q[1],q[0] )

In [36]:
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)

__ Initial State __
0.5 |00>  -0.5 |10>  -0.5 |01>  0.5 |11>

__ QFT __
0.5-0.5j |01>  0.5+0.5j |11>

__ Inverse QFT __
0.5 |00>  -0.5 |10>  -0.5 |01>  0.5 |11>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_79/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
/tmp/ipykernel_79/3986574663.py:337: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1(R_phis[int(j-k)], q[int(qubits-(k+1))], q[int(qubits-(j+1))] )

In [33]:
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)

_____ DFT† _____
State:  [0, 0, 0]  Amplitude:  0.35355
State:  [0, 0, 1]  Amplitude:  (0.25+0.25j)
State:  [0, 1, 0]  Amplitude:  0.35355j
State:  [0, 1, 1]  Amplitude:  (-0.25+0.25j)
State:  [1, 0, 0]  Amplitude:  -0.35355
State:  [1, 0, 1]  Amplitude:  (-0.25-0.25j)
State:  [1, 1, 0]  Amplitude:  (-0-0.35355j)
State:  [1, 1, 1]  Amplitude:  (0.25-0.25j)

Initial WF
1.0 |001>

_____ QFT _____
0.35355 |000>  0.25+0.25j |100>  0.35355j |010> -0.25+0.25j |110>  -0.35355 |001>  -0.25-0.25j |101>  -0.35355j |011> 0.25-0.25j |111>

/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_79/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )

In [34]:
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)

_____ DFT† _____
State:  [0, 0, 0]  Amplitude:  0.35355
State:  [0, 0, 1]  Amplitude:  (0.25+0.25j)
State:  [0, 1, 0]  Amplitude:  0.35355j
State:  [0, 1, 1]  Amplitude:  (-0.25+0.25j)
State:  [1, 0, 0]  Amplitude:  -0.35355
State:  [1, 0, 1]  Amplitude:  (-0.25-0.25j)
State:  [1, 1, 0]  Amplitude:  (-0-0.35355j)
State:  [1, 1, 1]  Amplitude:  (0.25-0.25j)

_____ QFT + SWAP _____
0.35355 |000>  -0.35355 |100>  0.35355j |010> -0.35355j |110> 0.25+0.25j |001>  -0.25-0.25j |101>  -0.25+0.25j |011>  0.25-0.25j |111>

/tmp/ipykernel_79/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
/tmp/ipykernel_79/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_79/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):

In [15]:
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])

marked state:  [1, 0]

____ Initial State (QFT) ____
0.5 |00>  0.5 |10>  0.5 |01>  0.5 |11>

____ Flip the Marked State ____
0.5 |00>  -0.5 |10>  0.5 |01>  0.5 |11>

____ QFT ____
0.5 |00>  0.5 |10>  -0.5 |01>  0.5 |11>

____ Flip the |00> state ____
-0.5 |00>  0.5 |10>  -0.5 |01>  0.5 |11>

____ QFT† ____
-1.0 |10>

/tmp/ipykernel_59/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_59/3986574663.py:337: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1(R_phis[int(j-k)], q[int(qubits-(k+1))], q[int(qubits-(j+1))] )


In [16]:
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, Aer, execute
import numpy as np
import math as m

In [17]:
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)

0.35355 |000>  -0.35355j |100> -0.35355 |010>  0.35355j |110> 0.35355 |001>  -0.35355j |101> -0.35355 |011>  0.35355j |111>

/tmp/ipykernel_59/4036861384.py:7: DeprecationWarning: The QuantumCircuit.u1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.p method instead, which acts identically.
qc.u1( 3*m.pi/2, q[0] )
/tmp/ipykernel_59/4036861384.py:8: DeprecationWarning: The QuantumCircuit.u1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.p method instead, which acts identically.
qc.u1( m.pi, q[1] )
/tmp/ipykernel_59/4036861384.py:9: DeprecationWarning: The QuantumCircuit.u1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.p method instead, which acts identically.
qc.u1( 0, q[2] )
/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):

In [18]:
q = QuantumRegister(3,name='q')
qc= QuantumCircuit(q,name='qc')
#------------------------------
qc.x( q[0] )
qc.x( q[1] )
QFT(qc,q,3)
Wavefunction(qc)

0.35355 |000>  -0.35355j |100> -0.35355 |010>  0.35355j |110> 0.35355 |001>  -0.35355j |101> -0.35355 |011>  0.35355j |111>

/tmp/ipykernel_59/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):

In [19]:
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])

_____ States to Add Together _____
1.0 |01>|10>

___ Sum Stored in |a> ___
1.0 |11>

/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_59/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
/tmp/ipykernel_59/4106535683.py:11: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi, qb[0], qa[0] )
/tmp/ipykernel_59/4106535683.py:12: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/2, qb[1], qa[0] )
/tmp/ipykernel_59/4106535683.py:13: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi, qb[1], qa[1] )
/tmp/ipykernel_59/3986574663.py:337: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1(R_phis[int(j-k)], q[int(qubits-(k+1))], q[int(qubits-(j+1))] )

In [20]:
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])

A: [0, 1]  B: [1, 0]
1.0 |11>

A: [0, 1]  B: [1, 1]
1.0 |00>

A: [1, 0]  B: [1, 0]
1.0 |00>

A: [1, 0]  B: [1, 1]
1.0 |01>

A: [1, 1]  B: [1, 0]
1.0 |01>

A: [1, 1]  B: [1, 1]
1.0 |10>

/tmp/ipykernel_59/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
/tmp/ipykernel_59/3780699860.py:21: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi, qb[0], qa[0] )
/tmp/ipykernel_59/3780699860.py:22: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/2, qb[1], qa[0] )
/tmp/ipykernel_59/3780699860.py:23: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi, qb[1], qa[1] )
/tmp/ipykernel_59/3986574663.py:337: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1(R_phis[int(j-k)], q[int(qubits-(k+1))], q[int(qubits-(j+1))] )
/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):

In [21]:
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])

_____ States to Add Together _____
1.0 |010>|11>

___ Sum Stored in |a> ___
1.0 |101>

/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_59/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
/tmp/ipykernel_59/1423019145.py:12: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/2, qb[0], qa[0] )
/tmp/ipykernel_59/1423019145.py:13: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/4, qb[1], qa[0] )
/tmp/ipykernel_59/1423019145.py:14: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi, qb[0], qa[1] )
/tmp/ipykernel_59/1423019145.py:15: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/2, qb[1], qa[1] )
/tmp/ipykernel_59/1423019145.py:16: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi, qb[1], qa[2] )
/tmp/ipykernel_59/3986574663.py:337: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1(R_phis[int(j-k)], q[int(qubits-(k+1))], q[int(qubits-(j+1))] )

In [22]:
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')

#--------------------------------------

print('\n___ Sum Stored in |a> ___')
Wavefunction(qc,systems=[len(A)+1,len(B)],show_systems=[True,False])

States to Add Together:  [0, 1, 1, 0]  +  [1, 1, 0, 1]

___ Sum Stored in |a> ___
1.0 |10011>

/tmp/ipykernel_59/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
/tmp/ipykernel_59/3986574663.py:401: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/(2**p), Qb[int(j)], Qa[0] )
/tmp/ipykernel_59/3986574663.py:406: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( m.pi/(2**p), Qb[int(jj)], Qa[int(i)] )
/tmp/ipykernel_59/3986574663.py:337: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1(R_phis[int(j-k)], q[int(qubits-(k+1))], q[int(qubits-(j+1))] )
/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):


Quantum Subtractor¶

In [25]:
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])

____ States to Subtract ____
1.0 |110>|011>

___ Difference Stored in |a> ___
1.0 |011>

/tmp/ipykernel_59/3986574663.py:99: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
qubits = int(m.log(len(statevec),2))
/tmp/ipykernel_59/3986574663.py:100: DeprecationWarning: The return type of saved statevectors has been changed from a numpy.ndarray to a qiskit.quantum_info.Statevector as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  np.asarray(object).
for i, comp in  enumerate(statevec): #i in np.arange( int(len(statevec)) ):
/tmp/ipykernel_59/3986574663.py:316: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( R_phis[k+1], q[int(j+k+1)], q[int(j)] )
/tmp/ipykernel_59/3118869378.py:13: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( -m.pi, qb[0], qa[0] )
/tmp/ipykernel_59/3118869378.py:14: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( -m.pi/2, qb[1], qa[0] )
/tmp/ipykernel_59/3118869378.py:15: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( -m.pi/4, qb[2], qa[0] )
/tmp/ipykernel_59/3118869378.py:16: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( -m.pi, qb[1], qa[1] )
/tmp/ipykernel_59/3118869378.py:17: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( -m.pi/2, qb[2], qa[1] )
/tmp/ipykernel_59/3118869378.py:18: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1( -m.pi, qb[2], qa[2] )
/tmp/ipykernel_59/3986574663.py:337: DeprecationWarning: The QuantumCircuit.cu1 method is deprecated as of 0.16.0. It will be removed no earlier than 3 months after the release date. You should use the QuantumCircuit.cp method instead, which acts identically.
qc.cu1(R_phis[int(j-k)], q[int(qubits-(k+1))], q[int(qubits-(j+1))] )