Browse Source

Move DETERMINISTIC to a library setting

master
Pete Shadbolt 8 years ago
parent
commit
cd589612b2
3 changed files with 10 additions and 7 deletions
  1. +2
    -0
      abp/__init__.py
  2. +4
    -6
      abp/graphstate.py
  3. +4
    -1
      tests/mock.py

+ 2
- 0
abp/__init__.py View File

@@ -1,3 +1,5 @@
# Alias some stuff to make imports cleaner # Alias some stuff to make imports cleaner
from abp.graphstate import GraphState from abp.graphstate import GraphState
from abp.qi import CircuitModel from abp.qi import CircuitModel

DETERMINISTIC = False

+ 4
- 6
abp/graphstate.py View File

@@ -7,6 +7,7 @@ This module implements Anders and Briegel's method for fast simulation of Cliffo
import itertools as it import itertools as it
import json, random import json, random
import qi, clifford, util import qi, clifford, util
import abp
from stabilizer import Stabilizer from stabilizer import Stabilizer




@@ -17,15 +18,13 @@ class GraphState(object):
Internally it uses the same dictionary-of-dictionaries data structure as ``networkx``. Internally it uses the same dictionary-of-dictionaries data structure as ``networkx``.
""" """


def __init__(self, data=(), deterministic=False, vop="identity"):
def __init__(self, data=(), vop="identity"):
""" Construct a ``GraphState`` """ Construct a ``GraphState``


:param data: An iterable of nodes used to construct the graph, or an integer -- the number of nodes, or a ``nx.Graph``. :param data: An iterable of nodes used to construct the graph, or an integer -- the number of nodes, or a ``nx.Graph``.
:param deterministic: If ``True``, the behaviour of the graph is deterministic up to but not including the choice of measurement outcome. This is slightly less efficient, but useful for testing. If ``False``, the specific graph representation will sometimes be random -- of course, all possible representations still map to the same state vector.
:param vop: The default VOP for new qubits. Setting ``vop="identity"`` initializes qubits in :math:`|+\\rangle`. Setting ``vop="hadamard"`` initializes qubits in :math:`|0\\rangle`. :param vop: The default VOP for new qubits. Setting ``vop="identity"`` initializes qubits in :math:`|+\\rangle`. Setting ``vop="hadamard"`` initializes qubits in :math:`|0\\rangle`.
""" """


self.deterministic = deterministic
self.adj, self.node = {}, {} self.adj, self.node = {}, {}
try: try:
# Cloning from a networkx graph # Cloning from a networkx graph
@@ -149,7 +148,7 @@ class GraphState(object):


""" """
others = set(self.adj[node]) - {avoid} others = set(self.adj[node]) - {avoid}
if self.deterministic:
if abp.DETERMINISTIC:
swap_qubit = min(others) if others else avoid swap_qubit = min(others) if others else avoid
else: else:
swap_qubit = others.pop() if others else avoid swap_qubit = others.pop() if others else avoid
@@ -352,7 +351,7 @@ class GraphState(object):


# Pick a friend vertex # Pick a friend vertex
if friend == None: if friend == None:
if self.deterministic:
if abp.DETERMINISTIC:
friend = sorted(self.adj[node].keys())[0] friend = sorted(self.adj[node].keys())[0]
else: else:
friend = next(self.adj[node].iterkeys()) friend = next(self.adj[node].iterkeys())
@@ -511,5 +510,4 @@ class GraphState(object):
g = GraphState() g = GraphState()
g.node = self.node.copy() g.node = self.node.copy()
g.adj = self.adj.copy() g.adj = self.adj.copy()
g.deterministic = self.deterministic
return g return g

+ 4
- 1
tests/mock.py View File

@@ -3,6 +3,7 @@ Mock graphs used for testing
""" """


import numpy as np import numpy as np
import abp
from abp import GraphState, clifford, qi from abp import GraphState, clifford, qi
from numpy import random from numpy import random
import nose import nose
@@ -14,6 +15,7 @@ except ImportError:
# We always run with A&B's CZ table when we are testing # We always run with A&B's CZ table when we are testing
clifford.use_old_cz() clifford.use_old_cz()



class AndersWrapper(graphsim.GraphRegister): class AndersWrapper(graphsim.GraphRegister):


""" A wrapper for A&B to make the interface identical and enable equality testing """ """ A wrapper for A&B to make the interface identical and enable equality testing """
@@ -52,7 +54,8 @@ class ABPWrapper(GraphState):
""" A wrapper for abp, just to ensure determinism """ """ A wrapper for abp, just to ensure determinism """


def __init__(self, nodes=[]): def __init__(self, nodes=[]):
super(ABPWrapper, self).__init__(nodes, deterministic=True, vop="hadamard")
abp.DETERMINISTIC = True
super(ABPWrapper, self).__init__(nodes, vop="hadamard")


def print_stabilizer(self): def print_stabilizer(self):
print self.to_stabilizer() print self.to_stabilizer()


Loading…
Cancel
Save