Browse Source

Nicer stabilizer tables

master
Pete Shadbolt 7 years ago
parent
commit
a9e17c5fef
3 changed files with 28 additions and 19 deletions
  1. +12
    -7
      abp/graphstate.py
  2. +9
    -9
      abp/qi.py
  3. +7
    -3
      abp/stabilizer.py

+ 12
- 7
abp/graphstate.py View File

@@ -1,3 +1,5 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" """
This module implements Anders and Briegel's method for fast simulation of Clifford circuits. This module implements Anders and Briegel's method for fast simulation of Clifford circuits.
""" """
@@ -391,8 +393,8 @@ class GraphState(object):
The output state is represented as a ``abp.qi.CircuitModel``:: The output state is represented as a ``abp.qi.CircuitModel``::


>>> print g.to_state_vector() >>> print g.to_state_vector()
|00000>: 0.18+0.00j
|00001>: 0.18+0.00j ...
|00000: 0.18+0.00j
|00001: 0.18+0.00j ...


""" """
if len(self.node) > 15: if len(self.node) > 15:
@@ -412,11 +414,14 @@ class GraphState(object):
Get the stabilizer representation of the state:: Get the stabilizer representation of the state::


>>> print g.to_stabilizer() >>> print g.to_stabilizer()
- X I I I I
I Z I I I
I I Z I I
I I I Z I
I I I I Z
0 1 2 3 100 200
------------------------------
X Z Z X I I
Z X Z I I I
Z Z X I I I
- Z I I Z I I
I I I I X Z
I I I I Z X


""" """
return Stabilizer(self) return Stabilizer(self)


+ 9
- 9
abp/qi.py View File

@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-


""" """
@@ -75,7 +75,7 @@ class CircuitModel(object):
self.state[0, 0] = 1 self.state[0, 0] = 1


def act_cz(self, control, target): def act_cz(self, control, target):
""" Act a CU somewhere """
""" Act a CU somewhere. """
control = 1 << control control = 1 << control
target = 1 << target target = 1 << target
for i in xrange(self.d): for i in xrange(self.d):
@@ -83,13 +83,13 @@ class CircuitModel(object):
self.state[i, 0] *= -1 self.state[i, 0] *= -1


def act_cnot(self, control, target): def act_cnot(self, control, target):
""" Act a CNOT """
""" Act a CNOT. """
self.act_hadamard(target) self.act_hadamard(target)
self.act_cz(control, target) self.act_cz(control, target)
self.act_hadamard(target) self.act_hadamard(target)


def act_hadamard(self, qubit): def act_hadamard(self, qubit):
""" Act a hadamard somewhere """
""" Act a hadamard somewhere. """
where = 1 << qubit where = 1 << qubit
output = np.zeros((self.d, 1), dtype=complex) output = np.zeros((self.d, 1), dtype=complex)
for i, v in enumerate(self.state): for i, v in enumerate(self.state):
@@ -99,7 +99,7 @@ class CircuitModel(object):
self.state = output self.state = output


def act_local_rotation(self, qubit, u): def act_local_rotation(self, qubit, u):
""" Act a local unitary somwhere """
""" Act a local unitary somwhere. """
where = 1 << qubit where = 1 << qubit
output = np.zeros((self.d, 1), dtype=complex) output = np.zeros((self.d, 1), dtype=complex)
for i, v in enumerate(self.state): for i, v in enumerate(self.state):
@@ -109,7 +109,7 @@ class CircuitModel(object):
self.state = output self.state = output


def act_circuit(self, circuit): def act_circuit(self, circuit):
""" Act a sequence of gates """
""" Act a sequence of gates. """
for node, operation in circuit: for node, operation in circuit:
if operation == "cz": if operation == "cz":
self.act_cz(*node) self.act_cz(*node)
@@ -117,8 +117,8 @@ class CircuitModel(object):
self.act_local_rotation(node, operation) self.act_local_rotation(node, operation)


def __eq__(self, other): def __eq__(self, other):
""" Check whether two quantum states are the same or not
UP TO A GLOBAL PHASE """
""" Check whether two quantum states are the same or not,
up to a global phase. """
a = normalize_global_phase(self.state) a = normalize_global_phase(self.state)
b = normalize_global_phase(other.state) b = normalize_global_phase(other.state)
return np.allclose(a, b) return np.allclose(a, b)
@@ -133,6 +133,6 @@ class CircuitModel(object):
real_frac = Fraction(str(term.real**2)).limit_denominator() real_frac = Fraction(str(term.real**2)).limit_denominator()
imag_sign = "+" if term.imag>=0 else "-" imag_sign = "+" if term.imag>=0 else "-"
imag_frac = Fraction(str(term.imag**2)).limit_denominator() imag_frac = Fraction(str(term.imag**2)).limit_denominator()
s += "|{}>: \t{}√{}\t{} i √{}\n".format(
s += "|{}: \t{}√{}\t{} i √{}\n".format(
label, real_sign, real_frac, imag_sign, imag_frac) label, real_sign, real_frac, imag_sign, imag_frac)
return s return s

+ 7
- 3
abp/stabilizer.py View File

@@ -35,13 +35,17 @@ class Stabilizer(object):


def __str__(self): def __str__(self):
""" Represent as a string """ """ Represent as a string """
s = ""
keys = map(str, self.tableau.keys())
w = max(len(k) for k in keys)
keys = [k.ljust(w) for k in keys]
s = " {}\n".format(" ".join(map(str, keys)))
s += " " + "-"*len(keys)*(w+2) + "\n"
for i in sorted(self.phases): for i in sorted(self.phases):
sign = self.phases[i] sign = self.phases[i]
sign = {1: " ", -1: " -", 1j: " i", -1j: "-i"}[sign] sign = {1: " ", -1: " -", 1j: " i", -1j: "-i"}[sign]
row = (self.tableau[i][j] for j in sorted(self.phases)) row = (self.tableau[i][j] for j in sorted(self.phases))
row = ("IXYZ"[i] for i in row)
row = " ".join(row)
row = ("IXYZ"[i].ljust(w) for i in row)
row = " ".join(row)
s += "{} {}\n".format(sign, row) s += "{} {}\n".format(sign, row)
return s return s



Loading…
Cancel
Save