Run with non-zero initial statevector in Blueqat 0.4.1
In Blueqat 0.4.1, you can now specify an arbitrary initial state to run the circuit.
Update Blueqat library
If you have an older version of Blueqat installed, update it as follows.
Copy
!pip install -U blueqat
Collecting blueqat
Downloading blueqat-0.4.1-py3-none-any.whl (60 kB)
[K |████████████████████████████████| 60 kB 577 kB/s eta 0:00:011
[?25hRequirement already satisfied, skipping upgrade: scipy>=1.1.0 in /opt/conda/lib/python3.8/site-packages (from blueqat) (1.4.1)
Requirement already satisfied, skipping upgrade: numpy~=1.12 in /opt/conda/lib/python3.8/site-packages (from blueqat) (1.19.1)
[31mERROR: openfermionblueqat 0.2.2 has requirement blueqat~=0.3.3, but you'll have blueqat 0.4.1 which is incompatible.[0m
Installing collected packages: blueqat
Attempting uninstall: blueqat
Found existing installation: blueqat 0.3.18
Uninstalling blueqat-0.3.18:
Display all output >>>
run! run! run!
By executing run(initial=vec), the circuit runs from the specified statevector.
Copy
import numpy as np
from blueqat import Circuit
# run without initial
c = Circuit().h[0].m[0]
c.run(shots=100) # => 50% each of 0 and 1
Counter({'0': 52, '1': 48})
Copy
# Prepare vec = (|0> - |1>)/√2
vec = np.array([1, -1]) / np.sqrt(2)
# You will get only '1' measurement because H(|0> - |1>)/√2 = |1>
c.run(shots=100, initial=vec)
Counter({'1': 100})
Dimension matching
The size of the state vector must be 2 ^ # of qubits. And the statevectors should be normalized, it is not checked in Blueqat, but unnormalized vector may cause unexpected results in measurements or other operations.
Copy
# When dimension is unmatching you get an error.
c.x[3].run(shots=100, initial=np.array([1, 0, 0, 0]))
Copy
# To make sure normalize, calc as follows.
# True: normalized.
np.allclose(np.vdot(vec, vec), 1)
The initial state vector can be created using numpy or other methods, but you can also use the results of the blueqat circuit. In this case, we need to run the circuit with a matching number of qubits.
Copy
# c1 uses only 1 qubit, but Circuit(2) assume it has 2 qubits.
c1 = Circuit(2).x[0]
c2 = Circuit(2).x[1]
c2.run(initial=c1.run())
array([0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j])
Quantum teleportation
blueqat is that when taking the Kronecker product of qubits, they are multiplied from the right (2nd⊗1st⊗0th)
Copy
# Alice and Bob share a bell state
bell = Circuit().h[0].cx[0, 1].run()
bell
# Communicate measured result from Alice to Bob
measured = shot.most_common()[0][0]
measured
'110'
Copy
# Bob:
bob = Circuit(3)
if measured[0] == '1':
bob.x[2]
if measured[1] == '1':
bob.z[2]
# Bob will take the measurements from the continuation of Alice's measurements
result = bob.run(initial=vec)
result
The statevector has been transferred to Bob. To check this, we just need to see that this state vector is the Kronecker product of the teleported state psi and the state that Alice has in hand.
Copy
alice_state = Circuit(2)
if measured[0] == '1':
alice_state.x[0]
if measured[1] == '1':
alice_state.x[1]
expect = np.kron(psi, alice_state.run())
expect