common.title

Docs
Quantum Circuit
TYTAN CLOUD

QUANTUM GAMING


Overview
Contact
Event
Project
Research

Terms of service (Web service)

Terms of service (Quantum and ML Cloud service)

Privacy policy


Sign in
Sign up
common.title

Quantum teleportation using Blueqat 0.4.1 feature

gyu-don

2021/03/11 03:45

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.

!pip install -U blueqat
Collecting blueqat
  Downloading blueqat-0.4.1-py3-none-any.whl (60 kB)
     |████████████████████████████████| 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)
ERROR: openfermionblueqat 0.2.2 has requirement blueqat~=0.3.3, but you'll have blueqat 0.4.1 which is incompatible.
Installing collected packages: blueqat
  Attempting uninstall: blueqat
    Found existing installation: blueqat 0.3.18
    Uninstalling blueqat-0.3.18:
      Successfully uninstalled blueqat-0.3.18
Successfully installed blueqat-0.4.1

run! run! run!

By executing run(initial=vec), the circuit runs from the specified statevector.

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})
# 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.

# When dimension is unmatching you get an error.
c.x[3].run(shots=100, initial=np.array([1, 0, 0, 0]))
# To make sure normalize, calc as follows.

# True: normalized.
np.allclose(np.vdot(vec, vec), 1)
True
# False: NOT normalized.
vec2 = np.array([1, 1])
np.allclose(np.vdot(vec2, vec2), 1)
False

Cascading statevector

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.

# 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$\otimes1st\otimes$0th)

# Alice and Bob share a bell state
bell = Circuit().h[0].cx[0, 1].run()
bell
array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])
# Teleport psi
psi = np.array([np.cos(0.4), 1j*np.sin(0.4)])
psi
array([0.92106099+0.j        , 0.        +0.38941834j])
# The state of the whole system is a Kronecker product of bell and psi
state = np.kron(bell, psi)
state
array([0.65128847+0.j        , 0.        +0.27536035j,
       0.        +0.j        , 0.        +0.j        ,
       0.        +0.j        , 0.        +0.j        ,
       0.65128847+0.j        , 0.        +0.27536035j])
# Alice:
alice = Circuit(3).cx[0, 1].h[0].m[0, 1]
# Get both statevector and shot
vec, shot = alice.run(returns="statevector_and_shots", shots=1, initial=state)
vec, shot
(array([0.        +0.j        , 0.        +0.j        ,
        0.        +0.j        , 0.        -0.38941834j,
        0.        +0.j        , 0.        +0.j        ,
        0.        +0.j        , 0.92106099+0.j        ]),
 Counter({'110': 1}))
# Communicate measured result from Alice to Bob
measured = shot.most_common()[0][0]
measured
'110'
# 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
array([ 0.        +0.j        ,  0.        +0.j        ,
        0.        +0.j        ,  0.92106099+0.j        ,
       -0.        +0.j        , -0.        +0.j        ,
       -0.        +0.j        ,  0.        +0.38941834j])

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.

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
array([0.        +0.j        , 0.        +0.j        ,
       0.        +0.j        , 0.92106099+0.j        ,
       0.        +0.j        , 0.        +0.j        ,
       0.        +0.j        , 0.        +0.38941834j])
# They're same!
np.allclose(result, expect)
True

© 2025, blueqat Inc. All rights reserved