def run3Drandom(size=4,tSteps=5,p=0.05,pLie=0.01,timespace=[1,1]):

   L=toric_lattice.PlanarLattice(size)
   PL=toric_lattice.Lattice3D(size)
   
   for i in range(tSteps):                     # loop over time      
                
        L.applyRandomErrors(p,p)
        L.measurePlaquettes(pLie)               
        L.measureStars(pLie)                    
        PL.addMeasurement(L)         # add the updated 2D lattice to 3D array

   L.measurePlaquettes(0)                      # measure one more layer with
   L.measureStars(0)                           # perfect stabilizers and add
   PL.addMeasurement(L)             # this to the parity Lattice

   PL.findAnyons()

   matchingX=perfect_matching.match_toric_3D(size,PL.anyon_positions_P,timespace)
   matchingZ=perfect_matching.match_toric_3D(size,PL.anyon_positions_S,timespace)

   flipsX=squashMatching(size,matchingX)
   flipsZ=squashMatching(size,matchingZ)
   
   L.apply_flip_array("Z",flipsZ)
   L.apply_flip_array("X",flipsX)

# uncomment to display the final error state of the array
 #  L.showArrayText("errors","X")

   return L.measure_logical()
def run2D(size=4, p=0.1):

    L = toric_lattice.PlanarLattice(size)

    L.applyRandomErrors(p, p)
    L.measureStars()
    L.measurePlaquettes()

    #L.showArray("errors","Z")
    #L.showArray("stabilizers","X")

    L.findAnyons()
    #print L.positions_anyons_P

    matchingX = perfect_matching.match_toric_2D(size, L.positions_anyons_P)
    matchingZ = perfect_matching.match_toric_2D(size, L.positions_anyons_S)

    #print matching

    L.apply_matching("X", matchingX)
    L.apply_matching("Z", matchingZ)

    #L.showArray("errors","Z")
    #L.showArray("errors","Z")

    return L.measure_logical()
def run3D(size=4,tSteps=5,errorVec=testvec4,timespace=[1,1],stabilizersNotComplete=0):

   L=toric_lattice.PlanarLattice(size)
   PL=toric_lattice.Lattice3D(size)

   xcount = 0
   zcount = 0 

   for i in range(tSteps):                     # loop over time                                                       

        L.measureNoisyStabilizers("plaquette",errorVec,stabilizersNotComplete)
        L.measureNoisyStabilizers("star",errorVec,stabilizersNotComplete)

        PL.addMeasurement(L)         # add the updated 2D lattice to 3D array
   
   L.measurePlaquettes(0)                      # measure one more layer with
   L.measureStars(0)                           # perfect stabilizers and add
   PL.addMeasurement(L)                        # this to the parity Lattice

   PL.findAnyons()

   matchingX=perfect_matching.match_toric_3D(size,PL.anyon_positions_P,timespace)
   matchingZ=perfect_matching.match_toric_3D(size,PL.anyon_positions_S,timespace)

   flipsX=squashMatching(size,matchingX)
   flipsZ=squashMatching(size,matchingZ)

   L.apply_flip_array("Z",flipsZ)
   L.apply_flip_array("X",flipsX)

   return L.measure_logical()
def run2D(size=4,p=0.1):

   L=toric_lattice.PlanarLattice(size)

   L.applyRandomErrors(p,p)
   L.measureStars()
   L.measurePlaquettes()
   
   #Uncomment display function for debugging
   #L.showArrayText("errors","Z")

   L.findAnyons()

   matchingX=perfect_matching.match_toric_2D(size,L.positions_anyons_P)
   matchingZ=perfect_matching.match_toric_2D(size,L.positions_anyons_S)

   L.apply_matching("X",matchingX)
   L.apply_matching("Z",matchingZ)

   return L.measure_logical()
def run3Dtest(size=8, tSteps=100, errorVec=testvec4, timespace=[1, 1]):

    errvec_file = "error_vectors/basic_0.txt"
    error_vector3, error_vector4 = [[] if x == {} else x[0.0]
                                    for x in load_errors.load(errvec_file)]
    errorVec = error_vector4

    #   print error_vector3
    #  print error_vector4

    L = toric_lattice.PlanarLattice(size)
    PL = toric_lattice.Lattice3D(size)

    L.applyRandomErrors(0, 0)  # leave this here for initialisation
    xcount = 0
    zcount = 0
    #t0=time.time()
    for i in range(tSteps):  # loop over time

        L.measureNoisyStabilizers("plaquette", errorVec)
        L.measureNoisyStabilizers("star", errorVec)

        #for p0,p1 in L.positions_Q:
        #  x,z = L.array[p0][p1]
        # if x ==-1: xcount +=1
        # if z ==-1: zcount +=1

        PL.addMeasurement(L)  # add the updated 2D lattice to 3D array


#   print xcount, zcount
#  t1 = time.time()
# print 'tsteps completed ',t1-t0

    L.measurePlaquettes(0)  # measure one more layer with
    L.measureStars(0)  # perfect stabilizers and add
    PL.addMeasurement(L)  # this to the parity Lattice

    PL.findAnyons()
    #   print 'P anyons', sum([len(_) for _ in PL.anyon_positions_P])
    #  print 'S anyons', sum([len(_) for _ in PL.anyon_positions_S])

    #print PL.anyon_positions_P
    # t2 = time.time()
    #print 'find anyons ',t2-t1

    matchingX = perfect_matching.match_toric_3D(size, PL.anyon_positions_P,
                                                timespace)
    # t3 = time.time()
    #print 'matching X ',t3-t2
    matchingZ = perfect_matching.match_toric_3D(size, PL.anyon_positions_S,
                                                timespace)

    flipsX = squashMatching(size, matchingX)
    flipsZ = squashMatching(size, matchingZ)
    #   t4 = time.time()
    #  print 'squash matching ',t4-t3
    #   L.showArray("errors","X")

    L.apply_flip_array("Z", flipsZ)
    L.apply_flip_array("X", flipsX)

    #  L.showArray("errors","X")
    #L.showArray("errors","Z")

    return L.measure_logical()