From 0b26721118d39ea7f212c5f361645a3bbcd2d22d Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Sun, 28 Feb 2016 18:18:45 +0000 Subject: [PATCH] Move operators to qi.py --- .gitignore | 1 + clifford.py | 26 +++++++++++++++----------- ct.txt | 1 - qi.py | 24 ++++++++++++++++++++++++ tests/test_clifford.py | 16 +++++----------- tt.txt | 1 - 6 files changed, 45 insertions(+), 24 deletions(-) delete mode 100644 ct.txt create mode 100644 qi.py delete mode 100644 tt.txt diff --git a/.gitignore b/.gitignore index 6c91b55..f39bd6d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +tables.pkl *.pdf papers/ # Project-specific diff --git a/clifford.py b/clifford.py index 25623ba..5105921 100644 --- a/clifford.py +++ b/clifford.py @@ -11,33 +11,37 @@ Following the prescription of Anders (thesis pg. 26): > indicated by the column header and the opposite sign otherwise. """ -from numpy import * +import numpy as np +from qi import * from tqdm import tqdm +import cPickle,os def find_up_to_phase(u): """ Find the index of a given u within a list of unitaries, up to a global phase """ global unitaries for i, t in enumerate(unitaries): for phase in range(8): - if allclose(t, exp(1j*phase*pi/4.)*u): + if np.allclose(t, np.exp(1j*phase*np.pi/4.)*u): return i, phase raise IndexError def construct_tables(): """ Constructs multiplication and conjugation tables """ - permutations = (id, ha, ph, ha*ph, ha*ph*ha, ha*ph*ha*ph) - signs = (id, px, py, pz) - unitaries = [p*s for p in permutations for s in signs] conjugation_table = [find_up_to_phase(u.H)[0] for i, u in enumerate(unitaries)] times_table = [[find_up_to_phase(u*v)[0] for v in unitaries] for u in tqdm(unitaries, "Building times-table")] + with open("tables.pkl", "w") as f: + cPickle.dump((conjugation_table, times_table), f) -id = matrix(eye(2, dtype=complex)) -px = matrix([[0, 1], [1, 0]], dtype=complex) -py = matrix([[0, -1j], [1j, 0]], dtype=complex) -pz = matrix([[1, 0], [0, -1]], dtype=complex) -ha = matrix([[1, 1], [1, -1]], dtype=complex) / sqrt(2) -ph = matrix([[1, 0], [0, 1j]], dtype=complex) +permutations = (id, ha, ph, ha*ph, ha*ph*ha, ha*ph*ha*ph) +signs = (id, px, py, pz) +unitaries = [p*s for p in permutations for s in signs] +# Build / reload lookup tables +if not os.path.exists("tables.pkl"): + construct_tables() + +with open("tables.pkl") as f: + conjugation_table, times_table = cPickle.load(f) diff --git a/ct.txt b/ct.txt deleted file mode 100644 index e976c4e..0000000 --- a/ct.txt +++ /dev/null @@ -1 +0,0 @@ -[0, 1, 2, 3, 4, 7, 6, 5, 11, 9, 10, 8, 20, 22, 23, 21, 17, 16, 18, 19, 12, 15, 13, 14] diff --git a/qi.py b/qi.py new file mode 100644 index 0000000..8421b47 --- /dev/null +++ b/qi.py @@ -0,0 +1,24 @@ + #!/usr/bin/python +# -*- coding: utf-8 -*- + +""" +Exposes a few basic QI operators +""" + +import numpy as np +from scipy.linalg import sqrtm + +id = np.matrix(np.eye(2, dtype=complex)) +px = np.matrix([[0, 1], [1, 0]], dtype=complex) +py = np.matrix([[0, -1j], [1j, 0]], dtype=complex) +pz = np.matrix([[1, 0], [0, -1]], dtype=complex) +ha = np.matrix([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2) +ph = np.matrix([[1, 0], [0, 1j]], dtype=complex) + +sqy = sqrtm(1j * py) +msqy = sqrtm(-1j * py) +sqz = sqrtm(1j * pz) +msqz = sqrtm(-1j * pz) +sqx = sqrtm(1j * px) +msqx = sqrtm(-1j * px) +paulis = (px, py, pz) diff --git a/tests/test_clifford.py b/tests/test_clifford.py index 45bc4cc..7b987f0 100644 --- a/tests/test_clifford.py +++ b/tests/test_clifford.py @@ -1,14 +1,8 @@ import clifford as lc from numpy import * from scipy.linalg import sqrtm +from qi import * -sqy = sqrtm(1j * lc.py) -msqy = sqrtm(-1j * lc.py) -sqz = sqrtm(1j * lc.pz) -msqz = sqrtm(-1j * lc.pz) -sqx = sqrtm(1j * lc.px) -msqx = sqrtm(-1j * lc.px) -paulis = (lc.px, lc.py, lc.pz) def identify_pauli(m): @@ -21,9 +15,9 @@ def identify_pauli(m): def test_find_up_to_phase(): """ Test that slightly suspicious function """ - assert lc.find_up_to_phase(lc.id) == (0, 0) - assert lc.find_up_to_phase(lc.px) == (1, 0) - assert lc.find_up_to_phase(exp(1j*pi/4.)*lc.ha) == (4, 7) + assert lc.find_up_to_phase(id) == (0, 0) + assert lc.find_up_to_phase(px) == (1, 0) + assert lc.find_up_to_phase(exp(1j*pi/4.)*ha) == (4, 7) def get_action(u): """ What does this unitary operator do to the Paulis? """ @@ -42,7 +36,7 @@ def test_we_have_24_matrices(): def test_we_have_all_useful_gates(): """ Check that all the interesting gates are included up to a global phase """ - common_us = lc.id, lc.px, lc.py, lc.pz, lc.ha, lc.ph, sqz, msqz, sqy, msqy, sqx, msqx + common_us = id, px, py, pz, ha, ph, sqz, msqz, sqy, msqy, sqx, msqx for u in common_us: lc.find_up_to_phase(u) diff --git a/tt.txt b/tt.txt deleted file mode 100644 index 430daff..0000000 --- a/tt.txt +++ /dev/null @@ -1 +0,0 @@ -[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], [1, 0, 3, 2, 7, 6, 5, 4, 10, 11, 8, 9, 15, 14, 13, 12, 17, 16, 19, 18, 22, 23, 20, 21], [2, 3, 0, 1, 6, 7, 4, 5, 9, 8, 11, 10, 13, 12, 15, 14, 19, 18, 17, 16, 23, 22, 21, 20], [3, 2, 1, 0, 5, 4, 7, 6, 11, 10, 9, 8, 14, 15, 12, 13, 18, 19, 16, 17, 21, 20, 23, 22], [4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 21, 20, 23, 22, 17, 16, 19, 18], [5, 4, 7, 6, 3, 2, 1, 0, 14, 15, 12, 13, 11, 10, 9, 8, 20, 21, 22, 23, 19, 18, 17, 16], [6, 7, 4, 5, 2, 3, 0, 1, 13, 12, 15, 14, 9, 8, 11, 10, 22, 23, 20, 21, 18, 19, 16, 17], [7, 6, 5, 4, 1, 0, 3, 2, 15, 14, 13, 12, 10, 11, 8, 9, 23, 22, 21, 20, 16, 17, 18, 19], [8, 9, 10, 11, 21, 20, 23, 22, 3, 2, 1, 0, 17, 16, 19, 18, 15, 14, 13, 12, 4, 5, 6, 7], [9, 8, 11, 10, 22, 23, 20, 21, 1, 0, 3, 2, 18, 19, 16, 17, 14, 15, 12, 13, 6, 7, 4, 5], [10, 11, 8, 9, 23, 22, 21, 20, 2, 3, 0, 1, 16, 17, 18, 19, 12, 13, 14, 15, 7, 6, 5, 4], [11, 10, 9, 8, 20, 21, 22, 23, 0, 1, 2, 3, 19, 18, 17, 16, 13, 12, 15, 14, 5, 4, 7, 6], [12, 13, 14, 15, 16, 17, 18, 19, 7, 6, 5, 4, 20, 21, 22, 23, 11, 10, 9, 8, 0, 1, 2, 3], [13, 12, 15, 14, 19, 18, 17, 16, 5, 4, 7, 6, 23, 22, 21, 20, 10, 11, 8, 9, 2, 3, 0, 1], [14, 15, 12, 13, 18, 19, 16, 17, 6, 7, 4, 5, 21, 20, 23, 22, 8, 9, 10, 11, 3, 2, 1, 0], [15, 14, 13, 12, 17, 16, 19, 18, 4, 5, 6, 7, 22, 23, 20, 21, 9, 8, 11, 10, 1, 0, 3, 2], [16, 17, 18, 19, 12, 13, 14, 15, 20, 21, 22, 23, 7, 6, 5, 4, 1, 0, 3, 2, 10, 11, 8, 9], [17, 16, 19, 18, 15, 14, 13, 12, 22, 23, 20, 21, 4, 5, 6, 7, 0, 1, 2, 3, 8, 9, 10, 11], [18, 19, 16, 17, 14, 15, 12, 13, 21, 20, 23, 22, 6, 7, 4, 5, 2, 3, 0, 1, 9, 8, 11, 10], [19, 18, 17, 16, 13, 12, 15, 14, 23, 22, 21, 20, 5, 4, 7, 6, 3, 2, 1, 0, 11, 10, 9, 8], [20, 21, 22, 23, 11, 10, 9, 8, 19, 18, 17, 16, 0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15], [21, 20, 23, 22, 8, 9, 10, 11, 17, 16, 19, 18, 3, 2, 1, 0, 5, 4, 7, 6, 14, 15, 12, 13], [22, 23, 20, 21, 9, 8, 11, 10, 18, 19, 16, 17, 1, 0, 3, 2, 7, 6, 5, 4, 15, 14, 13, 12], [23, 22, 21, 20, 10, 11, 8, 9, 16, 17, 18, 19, 2, 3, 0, 1, 6, 7, 4, 5, 13, 12, 15, 14]]