/
testRepLib.py
66 lines (50 loc) · 2.39 KB
/
testRepLib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import unittest
import pygsti
import numpy as np
import warnings
import pickle
import os
from pygsti.construction import std1Q_XYI as std
from ..testutils import BaseTestCase, compare_files, temp_files
try:
from pygsti.objects import fastreplib as replib
except ImportError:
from pygsti.objects import replib
# This class is for unifying some models that get used in this file and in testGateSets2.py
class RepLibTestCase(BaseTestCase):
def setUp(self):
super(RepLibTestCase, self).setUp()
def testRepLib_basic(self):
#just some simple tests of replib functions for now
x = np.zeros(4,'d')
staterep = replib.DMStateRep(x) # state rep
x = np.zeros(4,'d')
erep = replib.DMEffectRep_Dense(x)
self.assertAlmostEqual(erep.probability(staterep), 0.0)
g = np.zeros((4,4),'d')
grep = replib.DMOpRep_Dense(g)
staterep2 = grep.acton(staterep)
self.assertEqual(type(staterep2), replib.DMStateRep)
def testRepLib_map(self):
#Just test a Model with a "map" simtype to exercise the replib
mdl = std.target_model()
mdl.set_simtype("map")
probs = mdl.probs(('Gx','Gx'))
self.assertAlmostEqual(probs['0'], 0.0)
self.assertAlmostEqual(probs['1'], 1.0)
probs2 = mdl.bulk_probs([('Gx',),('Gx','Gx'),('Gx','Gx','Gy')])
self.assertAlmostEqual(probs2[('Gx',)]['0'], 0.5)
self.assertAlmostEqual(probs2[('Gx',)]['1'], 0.5)
self.assertAlmostEqual(probs2[('Gx','Gx')]['0'], 0.0)
self.assertAlmostEqual(probs2[('Gx','Gx')]['1'], 1.0)
self.assertAlmostEqual(probs2[('Gx','Gx','Gy')]['0'], 0.5)
self.assertAlmostEqual(probs2[('Gx','Gx','Gy')]['1'], 0.5)
#LATER: save & check outputs of dprobs
dprobs = mdl.bulk_dprobs([('Gx',),('Gx','Gx'),('Gx','Gx','Gy')])
#RUN TO SAVE outputs
if os.environ.get('PYGSTI_REGEN_REF_FILES','no').lower() in ("yes","1","true","v2"): # "v2" to only gen version-dep files
pickle.dump(dprobs, open(compare_files + "/repLib_dprobs%s.pkl" % self.versionsuffix,'wb'))
compare = pickle.load(open(compare_files + "/repLib_dprobs%s.pkl" % self.versionsuffix,'rb'))
for opstr in dprobs:
for outcomeLbl in dprobs[opstr]:
self.assertArraysAlmostEqual(dprobs[opstr][outcomeLbl], compare[opstr][outcomeLbl])