Unverified Commit 916f1777 authored by dwuggh's avatar dwuggh
Browse files

use float64 to improve efficiency

parent 5acee0cd
......@@ -81,17 +81,17 @@ class DensityOperator(QOperator):
# measurement channel
# E_0: measurement result 1, E_1: measurement result -1
def measure_x(q = 0):
E_0 = 0.5 * np.array([[1, 1], [1, 1]], dtype=np.complex64)
E_1 = 0.5 * np.array([[1, -1], [-1, 1]], dtype=np.complex64)
E_0 = 0.5 * np.array([[1, 1], [1, 1]], dtype=np.float64)
E_1 = 0.5 * np.array([[1, -1], [-1, 1]], dtype=np.float64)
return QChannel([QOperator([q], E_0), QOperator([q], E_1)])
def measure_z(q = 0):
E_0 = np.array([[1, 0], [0, 0]], dtype=np.complex64)
E_1 = np.array([[0, 0], [0, 1]], dtype=np.complex64)
E_0 = np.array([[1, 0], [0, 0]], dtype=np.float64)
E_1 = np.array([[0, 0], [0, 1]], dtype=np.float64)
return QChannel([QOperator([q], E_0), QOperator([q], E_1)])
# generate bell pair in werner form
def bell_pair(p_n, qubits = [0, 1]):
Φ = np.array([1, 0, 0, 1], dtype=np.complex64) / 2 ** 0.5
Φ = np.array([1, 0, 0, 1], dtype=np.float64) / 2 ** 0.5
mat = (1 - 4 / 3 * p_n) * np.outer(Φ, Φ) + (p_n / 3) * np.identity(4)
return DensityOperator(qubits, mat)
import numpy as np
from numpy import dtype
from utils import *
class QOperator(object):
def __init__(self, qubits, operator: np.ndarray):
self.qubits = np.array(qubits)
self.operator = np.array(operator, dtype=np.complex64)
self.operator = np.array(operator, dtype=np.float64)
def qnum(self):
return self.qubits.size
......@@ -31,7 +30,7 @@ class QOperator(object):
# G = I ⊗ G
def broadcast(self, qnum: int):
dim = 2 ** qnum
big_operator = np.zeros((dim, dim), dtype=np.complex64)
big_operator = np.zeros((dim, dim), dtype=np.float64)
for i in range(dim):
for j in range(dim):
di = np.flip(get_bin_digits(i, qnum))
......@@ -47,7 +46,7 @@ class QOperator(object):
if id == 1 and not contains(self.qubits, k) and di[k] != dj[k]:
id = 0
val = np.complex64(id) * self.operator[gi, gj]
val = np.float64(id) * self.operator[gi, gj]
big_operator[i, j] = val
return big_operator
......@@ -55,7 +54,7 @@ class QOperator(object):
# qubits_new = np.fromiter((x for x in qubits if not contains(self.qubits, x)), dtype=np.int32)
qnum = len(qubits)
dim = 2 ** qnum
big_operator = np.zeros((dim, dim), dtype=np.complex64)
big_operator = np.zeros((dim, dim), dtype=np.float64)
# occupied qubits of the original operator
# we need to respect the order of self.qubits in `qubits`
......@@ -90,7 +89,7 @@ class QOperator(object):
if id == 1 and not contains(occupy, k) and di[k] != dj[k]:
id = 0
val = np.complex64(id) * self.operator[gi, gj]
val = np.float64(id) * self.operator[gi, gj]
big_operator[i, j] = val
return QOperator(qubits, big_operator)
# def rearrange(self, qubits_new):
......@@ -99,7 +98,8 @@ class QOperator(object):
# get adjoint operator
def dagger(self):
t = self.operator.transpose()
t = t.conj()
# NOTE only need to transpose since σ_y is also real in our definition.
# t = t.conj()
return QOperator(self.qubits, t)
def partial_trace(self, traced_qubits):
......@@ -111,7 +111,7 @@ class QOperator(object):
dim_new = 2 ** len(qubits_reserved)
# the traced matrix
mat_new = np.zeros((dim_new, dim_new), dtype=np.complex64)
mat_new = np.zeros((dim_new, dim_new), dtype=np.float64)
# occupied qubits of the original operator
occupy_traced = []
......@@ -172,14 +172,19 @@ def pauli_0(q=0):
def pauli_x(q = 0):
return QOperator([q], np.array([[0, 1], [1, 0]]))
# NOTE Since channels has the form of Kraus operator, global phase can be safely ignored.
def pauli_y(q = 0):
return QOperator([q], np.array([[0, -1], [1, 0]]))
# this is the self=-adjoint version of σ_y, which we will not use.
def pauli_y_exact(q = 0):
return QOperator([q], np.array([[0, 0 - 1j], [0 + 1j, 0]]))
def pauli_z(q = 0):
return QOperator([q], np.array([[1, 0], [0, -1]]))
def hadamard(q = 0):
return QOperator([q], 2**(-0.5) * np.array([[1, 1], [1, -1]], dtype=np.complex64))
return QOperator([q], 2**(-0.5) * np.array([[1, 1], [1, -1]], dtype=np.float64))
σ_x = pauli_x
σ_y = pauli_y
......
......@@ -4,6 +4,7 @@ from utils import *
from QOperator import *
from QChannel import *
from DensityOperator import *
from fidelity import *
......@@ -57,28 +58,32 @@ q3 ---∎-----X-----------Z-------⊤-- -----Z-------- --M(x)
data2 -----------------------------σ-- -------------- ------
'''
def nickerson_2(ρ: DensityOperator, err_model: ErrorModel, data1, data2, q1, q2, q3 ,q4, pauli, stringent_plus = True):
# for efficiency improvement
ρ1 = bell_pair(err_model.p_n, [q1, q3])
ρ2 = bell_pair(err_model.p_n, [q2, q4])
ρ.merge(ρ1)
ρ.merge(ρ2)
ρ1.merge(ρ2)
c1 = cnot([q2, q1])
c2 = cnot([q4, q3])
ρ.evolution(c1, err_model.p_g)
ρ.evolution(c2, err_model.p_g)
ρ1.evolution(c1, err_model.p_g)
ρ1.evolution(c2, err_model.p_g)
ρ.bell_measure(q2, q4, 'x', err_model.p_m)
ρ1.bell_measure(q2, q4, 'x', err_model.p_m)
ρ3 = bell_pair(err_model.p_n, [q2, q4])
ρ.merge(ρ2)
ρ1.merge(ρ3)
c3 = cphase([q2, q1])
c4 = cphase([q4, q3])
ρ.evolution(c3, err_model.p_g)
ρ.evolution(c4, err_model.p_g)
ρ1.evolution(c3, err_model.p_g)
ρ1.evolution(c4, err_model.p_g)
ρ.bell_measure(q2, q4, 'x', err_model.p_m)
ρ1.bell_measure(q2, q4, 'x', err_model.p_m)
# now, take ρ into consideration
ρ.merge(ρ1)
c5 = cpauli(pauli, [q1, data1])
c6 = cpauli(pauli, [q3, data2])
......
......@@ -27,15 +27,6 @@ D: 9, 10, 11
# np.array([[ 0.12499996+0.j, 0.12499996+0.j, 0.12499996+0.j,
# -0.12499996+0.j],
# [ 0. +0.j, 0. +0.j, 0. +0.j,
# 0. +0.j],
# [ 0. +0.j, 0. +0.j, 0. +0.j,
# 0. +0.j],
# [ 0.12499996+0.j, 0.12499996+0.j, 0.12499996+0.j,
# -0.12499996+0.j]], dtype=np.complex64)
def test_1():
np.set_printoptions(edgeitems=16, linewidth=200,
# formatter=dict(float=lambda x: "%.3g" % x)
......@@ -80,9 +71,9 @@ if __name__ == "__main__":
# a = test_1()
# test_3()
np.set_printoptions(edgeitems=16, linewidth=200,
formatter=dict(complexfloat=lambda x: "%5.6g" % x)
formatter=dict(float=lambda x: "%5.6g" % x)
)
err_model = cc.ErrorModel()
ρ = cc.make_bell(err_model, False, False)
err_model = cc.ErrorModel(0.1, 0.01)
ρ = cc.make_bell(err_model, True, False)
ρ.print()
# test_2()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment