Examples using gateraid package
[1]:
# Import relevant modules
from gateraid import * # To provide examples of the gateraid package, we install all the modules
import numpy as np
Using quantum states
[2]:
# Initialise a quantum state
q = QuantumState(np.array([1, 0, 0, 1])/np.sqrt(2))
print(q)
State:
0.7071+0j|00> +
0+0j|01> +
0+0j|10> +
0.7071+0j|11>
Common two-qubit gates
[3]:
# Initialise some common gates
# Control-not gate with control on qubit 0 and target on qubit 1
CX = make_CX()
# Control-phase gate with anti-control on qubit 1 and target on qubit 0
CZ = make_CZ(False, True)
# SWAP gate
SWAP = make_SWAP()
[4]:
# Apply the CX gate (expect 1/sqrt(2) (|00> + |11>) --> 1/sqrt(2) (|00> + |10>))
print(f'INITIAL STATE: \n {q} \n')
print(f'FINAL STATE: \n {CX(q)}')
INITIAL STATE:
State:
0.7071+0j|00> +
0+0j|01> +
0+0j|10> +
0.7071+0j|11>
FINAL STATE:
State:
0.7071+0j|00> +
0+0j|01> +
0.7071+0j|10> +
0+0j|11>
[5]:
# Apply the anti-controlled, reversed CZ gate (expect 1/sqrt(2) (|00> + |10>) --> 1/sqrt(2) (|00> - |10>))
print(f'INITIAL STATE: \n {q} \n')
print(f'FINAL STATE: \n {CZ(q)}')
INITIAL STATE:
State:
0.7071+0j|00> +
0+0j|01> +
0.7071+0j|10> +
0+0j|11>
FINAL STATE:
State:
0.7071+0j|00> +
0+0j|01> +
-0.7071+0j|10> +
0+0j|11>
[6]:
# Apply the SWAP gate (expect 1/sqrt(2) (|00> - |10>) --> 1/sqrt(2) (|00> - |01>))
print(f'INITIAL STATE: \n {q} \n')
print(f'FINAL STATE: \n {SWAP(q)}')
INITIAL STATE:
State:
0.7071+0j|00> +
0+0j|01> +
-0.7071+0j|10> +
0+0j|11>
FINAL STATE:
State:
0.7071+0j|00> +
-0.7071+0j|01> +
0+0j|10> +
0+0j|11>
General and customised two-qubit gates
[7]:
# Re-initialise the quantum state
q = QuantumState(np.array([1, 1, 0, 0])/np.sqrt(2))
print(q)
State:
0.7071+0j|00> +
0.7071+0j|01> +
0+0j|10> +
0+0j|11>
[8]:
# Initialise a local NOT gate acting on qubit 0
X = make_X()
XI = LocalUnitary(X, 'X', 0)
[9]:
# Apply the IX gate (expect 1/sqrt(2) (|00> + |01>) --> 1/sqrt(2) (|10> + |11>))
print(f'INITIAL STATE: \n {q} \n')
print(f'FINAL STATE: \n {XI(q)}')
INITIAL STATE:
State:
0.7071+0j|00> +
0.7071+0j|01> +
0+0j|10> +
0+0j|11>
FINAL STATE:
State:
0+0j|00> +
0+0j|01> +
0.7071+0j|10> +
0.7071+0j|11>
[10]:
# Initialise a control-Hadamard gate by customising the ControlledUnitary class
CH = ControlledUnitary(make_H(), 'CH')
[11]:
# Apply the CH gate (expect 1/sqrt(2) (|10> + |11>) --> |10>)
print(f'INITIAL STATE: \n {q} \n')
print(f'FINAL STATE: \n {CH(q)}')
INITIAL STATE:
State:
0+0j|00> +
0+0j|01> +
0.7071+0j|10> +
0.7071+0j|11>
FINAL STATE:
State:
0+0j|00> +
0+0j|01> +
1+0j|10> +
0+0j|11>
[12]:
# Initialise a random 2-qubit unitary gate
coef = np.random.rand(16)
pauli_dict = {'II': coef[0], 'IX': coef[1], 'IY': coef[2], 'IZ': coef[3], 'XI': coef[4], 'XX': coef[5], 'XY': coef[6], 'XZ': coef[7], 'YI': coef[8], 'YX': coef[9], 'YY': coef[10], 'YZ': coef[11], 'ZI': coef[12], 'ZX': coef[13], 'ZY': coef[14], 'ZZ': coef[15]}
U = GeneralUnitary(pauli_dict)
[13]:
# Apply the random gate
print(f'INITIAL STATE: \n {q} \n')
print(f'FINAL STATE: \n {U(q)}')
INITIAL STATE:
State:
0+0j|00> +
0+0j|01> +
1+0j|10> +
0+0j|11>
FINAL STATE:
State:
-0.04894+0.8219j|00> +
0.1308+0.2922j|01> +
-0.3178-0.3103j|10> +
-0.1481-0.01783j|11>
[ ]: