Some parts of pygsti
are works-in-progress. Here, we investigate how to do the task of "model selection" within GST, essentially answering the question "Can we do a better job of modeling the experiment by changing the assumptions within GST?".
from __future__ import print_function
import pygsti
#Load gateset and some string lists
gs_target = pygsti.io.load_gateset("tutorial_files/Example_Gateset.txt")
fiducialList = pygsti.io.load_gatestring_list("tutorial_files/Example_FiducialList.txt")
germList = pygsti.io.load_gatestring_list("tutorial_files/Example_GermsList.txt")
specs = pygsti.construction.build_spam_specs(fiducialList)
expList = [1,2,4]
#Create some testing gate string lists
lgstList = pygsti.construction.list_lgst_gatestrings(specs, gs_target.gates.keys())
lsgstLists = [ lgstList[:] ]
for exp in expList:
gsList = pygsti.construction.create_gatestring_list(
"f0+germ*exp+f1", f0=fiducialList, f1=fiducialList,
germ=germList, exp=exp, order=['germ','f0','f1'])
lsgstLists.append( lsgstLists[-1] + gsList )
dsList = pygsti.remove_duplicates( lsgstLists[-1] )
#Test on fake data by depolarizing target set, increasing its dimension,
# and adding leakage to the gates into the new dimension.
gs_dataGen4 = gs_target.depolarize(gate_noise=0.1)
gs_dataGen5 = gs_dataGen4.increase_dimension(5)
leakGate = pygsti.construction.build_gate( [2,1],[('Q0',),('L0',)] , "LX(pi/4.0,0,2)","gm") # X(pi,Q0)*LX(pi,0,2)
gs_dataGen5['Gx'] = pygsti.objects.compose( gs_dataGen5['Gx'], leakGate)
gs_dataGen5['Gy'] = pygsti.objects.compose( gs_dataGen5['Gy'], leakGate)
print(gs_dataGen5.gates.keys())
#Some debugging...
#NOTE: with LX(pi,0,2) above, dim 5 test will choose a dimension 3 gateset, which may be sensible
# looking at the gate matrices in this case... but maybe LX(pi,...) is faulty?
#print(gs_dataGen4)
#print(gs_dataGen5)
#Jmx = GST.JOps.jamiolkowski_iso(gs_dataGen4['Gx'])
#Jmx = GST.JOps.jamiolkowski_iso(gs_dataGen5['Gx'],dimOrStateSpaceDims=[2,1])
#print("J = \n",Jmx)
#print("evals = ",eigvals(Jmx))
dsFake4 = pygsti.construction.generate_fake_data(gs_dataGen4, dsList, nSamples=1000000, sampleError="binomial", seed=1234)
dsFake5 = pygsti.construction.generate_fake_data(gs_dataGen5, dsList, nSamples=1000000, sampleError="binomial", seed=1234)
odict_keys(['Gi', 'Gx', 'Gy'])
print("Number of gates = ",len(gs_target.gates.keys()))
print("Number of fiducials =",len(fiducialList))
print("Maximum length for a gate string in ds =",max(map(len,dsList)))
print("Number of LGST strings = ",len(lgstList))
print("Number of LSGST strings = ",map(len,lsgstLists))
Number of gates = 3 Number of fiducials = 6 Maximum length for a gate string in ds = 30 Number of LGST strings = 92 Number of LSGST strings = <map object at 0x106a6cb38>
#Run LGST to get an initial estimate for the gates in gs_target based on the data in ds
# NOTE: with nSamples less than 1M (100K, 10K, 1K) this routine will choose a higher-than-4 dimensional gateset
ds = dsFake4
gs_lgst4 = pygsti.do_lgst(ds, specs, targetGateset=gs_target, svdTruncateTo=4, verbosity=3)
gs_lgst6 = pygsti.do_lgst(ds, specs, targetGateset=gs_target, svdTruncateTo=6, verbosity=3)
#Print chi^2 of 4-dim and 6-dim estimates
chiSq4 = pygsti.chi2(ds, gs_lgst4, lgstList, minProbClipForWeighting=1e-4)
chiSq6 = pygsti.chi2(ds, gs_lgst6, lgstList, minProbClipForWeighting=1e-4)
print("LGST dim=4 chiSq = ", chiSq4)
print("LGST dim=6 chiSq = ", chiSq6)
# Least squares GST with model selection
gs_lsgst = pygsti.do_iterative_mc2gst_with_model_selection(ds, gs_lgst4, 1, lsgstLists, verbosity=2,
minProbClipForWeighting=1e-3, probClipInterval=(-1e5,1e5))
--- LGST --- Singular values of I_tilde (truncating to first 4 of 6) = 3.00262981818 0.825857216429 0.671539150077 0.664532662258 0.000838614351546 0.000292355239716 Singular values of target I_tilde (truncating to first 4 of 6) = 3.0 1.0 1.0 1.0 2.46365884775e-16 9.80956846773e-17 Resulting gate set: rho0 = 0.7072 -0.0214 0.0213 0.7510 E0 = 0.6847 0.0028 -0.0026 -0.6446 Gi = 1.0002 0 0.0003 -0.0001 -0.0042 0.9000 0.0005 -0.0004 0.0032 -0.0018 0.8995 -0.0009 -0.0030 0.0002 0.0013 0.8999 Gx = 1.0001 -0.0002 0 0 -0.0037 0.8982 0.0055 0.0005 0.0006 -0.0014 -0.0052 -1.0001 -0.0624 0.0070 0.8104 0.0065 Gy = 1.0003 0.0002 0 -0.0003 -0.0001 -0.0066 0.0002 0.9989 0.0041 0.0069 0.8989 0 -0.0625 -0.8098 -0.0065 0.0067 --- LGST --- Singular values of I_tilde (truncating to first 6 of 6) = 3.00262981818 0.825857216429 0.671539150077 0.664532662258 0.000838614351546 0.000292355239716 Singular values of target I_tilde (truncating to first 4 of 6) = 3.0 1.0 1.0 1.0 2.46365884775e-16 9.80956846773e-17 LGST: Padding target B with sqrt of low singular values of I_tilde: [ 0.00083861 0.00029236] Resulting gate set: rho0 = 0.7072 -0.0214 0.0213 0.7510 0.0022 -0.0087 E0 = 0.6847 0.0028 -0.0026 -0.6446 0.0123 0.0050 Gi = 1.0002 0 0.0003 -0.0001 -0.0047 0.0100 -0.0042 0.9000 0.0005 -0.0004 0.0027 0.0341 0.0032 -0.0018 0.8995 -0.0009 0.0164 -0.0220 -0.0030 0.0002 0.0013 0.8999 0.0359 0.0568 0.0012 -0.0156 0.0249 -0.0288 0.4451 -0.8206 0.0168 -0.0497 0.0435 0.0986 -0.8074 -2.7233 Gx = 1.0001 -0.0002 0 0 0.0034 0.0250 -0.0037 0.8982 0.0055 0.0005 -0.0199 0.0606 0.0006 -0.0014 -0.0052 -1.0001 -0.0254 0.0212 -0.0624 0.0070 0.8104 0.0065 0.0003 0.0024 -0.0076 -0.0219 -0.0256 0.0008 -0.0984 -1.9243 0.0070 0.0435 0.0193 -0.0007 0.0558 -0.1647 Gy = 1.0003 0.0002 0 -0.0003 0.0115 -0.0010 -0.0001 -0.0066 0.0002 0.9989 0.0597 -0.0196 0.0041 0.0069 0.8989 0 -0.0091 -0.0682 -0.0625 -0.8098 -0.0065 0.0067 0.0225 -0.0122 0.0132 0.0329 0.0020 -0.0336 -0.7649 -0.4999 0.0009 -0.0169 0.0394 0.0067 0.4817 0.5269 LGST dim=4 chiSq = 377.497001528 LGST dim=6 chiSq = 974.596182221 --- Iterative MC2GST: Iter 1 of 4 92 gate strings ---: --- Minimum Chi^2 GST with model selection (starting dim = 4) --- Dim 4: chi^2 = 61.7228, nGateStrings=92, nParams=56 (so expected mean = 36) Rejected dim 3: chi^2 = 5.51525e+06 (+5.51519e+06 w.r.t. expected mean of 92 strings - 33 params = 59) (dChi^2=5515185, 2*dParams=-46) Rejected dim 5: chi^2 = 38.4174 (+31.4174 w.r.t. expected mean of 92 strings - 85 params = 7) (dChi^2=-23, 2*dParams=58) --- Iterative MC2GST: Iter 2 of 4 488 gate strings ---: --- Minimum Chi^2 GST with model selection (starting dim = 4) --- Dim 4: chi^2 = 438.344, nGateStrings=488, nParams=56 (so expected mean = 432) Rejected dim 3: chi^2 = 4.86648e+07 (+4.86644e+07 w.r.t. expected mean of 488 strings - 33 params = 455) (dChi^2=48664372, 2*dParams=-46) Rejected dim 5: chi^2 = 393.935 (-9.06513 w.r.t. expected mean of 488 strings - 85 params = 403) (dChi^2=-44, 2*dParams=58) --- Iterative MC2GST: Iter 3 of 4 884 gate strings ---: --- Minimum Chi^2 GST with model selection (starting dim = 4) --- Dim 4: chi^2 = 866.447, nGateStrings=884, nParams=56 (so expected mean = 828) Rejected dim 3: chi^2 = 7.23237e+07 (+7.23228e+07 w.r.t. expected mean of 884 strings - 33 params = 851) (dChi^2=72322819, 2*dParams=-46) Selected dim 5: chi^2 = 799.239 (+0.239148 w.r.t. expected mean of 884 strings - 85 params = 799) (dChi^2=-67, 2*dParams=58) Rejected dim 6: chi^2 = 735.018 (-28.9821 w.r.t. expected mean of 884 strings - 120 params = 764) (dChi^2=-64, 2*dParams=70) --- Iterative MC2GST: Iter 4 of 4 1280 gate strings ---: --- Minimum Chi^2 GST with model selection (starting dim = 5) --- Dim 5: chi^2 = 1194.99, nGateStrings=1280, nParams=85 (so expected mean = 1195) Rejected dim 4: chi^2 = 1258.19 (+34.1897 w.r.t. expected mean of 1280 strings - 56 params = 1224) (dChi^2=63, 2*dParams=-58) Rejected dim 6: chi^2 = 1140.63 (-19.3747 w.r.t. expected mean of 1280 strings - 120 params = 1160) (dChi^2=-54, 2*dParams=70)
print(gs_lsgst)
rho0 = 0.7073 -0.0210 0.0248 0.7505 -0.0032 E0 = 0.6839 0.0030 -0.0043 -0.6443 0.0077 Gi = 1.0000 0 0 0 0.0009 -0.0032 0.8998 0.0001 0.0003 0.0105 0.0039 0 0.8998 0.0002 -0.0034 -0.0036 -0.0005 0.0002 0.9000 0.0146 0.0078 0.0208 0.0027 -0.0004 0.2010 Gx = 1.0000 0 0.0001 -0.0002 0 -0.0031 0.8999 0.0053 0.0055 -0.0091 0.0030 0.0043 -0.0091 -1.0049 -0.0089 -0.0669 0.0055 0.8064 0.0095 -0.0209 0.0006 -0.0070 0.0014 -0.0113 1.0758 Gy = 1.0000 -0.0001 0 -0.0002 0.0004 0.0060 -0.0052 0.0018 1.0064 -0.0114 0.0041 0.0036 0.8999 0.0021 0.0037 -0.0600 -0.8047 -0.0105 0.0055 -0.0098 0.0086 -0.0037 -0.0082 0.0147 0.1892
#Run LGST to get an initial estimate for the gates in gs_target based on the data in ds
ds = dsFake5
gs_lgst4 = pygsti.do_lgst(ds, specs, targetGateset=gs_target, svdTruncateTo=4, verbosity=3)
gs_lgst6 = pygsti.do_lgst(ds, specs, targetGateset=gs_target, svdTruncateTo=6, verbosity=3)
#Print chi^2 of 4-dim and 6-dim estimates
chiSq4 = pygsti.chi2(ds, gs_lgst4, lgstList, minProbClipForWeighting=1e-2)
chiSq6 = pygsti.chi2(ds, gs_lgst6, lgstList, minProbClipForWeighting=1e-2)
print("LGST dim=4 chiSq = ", chiSq4)
print("LGST dim=6 chiSq = ", chiSq6)
# Least squares GST with model selection
gs_lsgst = pygsti.do_iterative_mc2gst_with_model_selection(ds, gs_lgst4, 1, lsgstLists, verbosity=2, minProbClipForWeighting=1e-3, probClipInterval=(-1e5,1e5), useFreqWeightedChiSq=False, regularizeFactor=1.0, check=False, check_jacobian=False)
--- LGST --- Singular values of I_tilde (truncating to first 4 of 6) = 3.56525557364 0.755465956932 0.496239591347 0.487203220172 0.00691003018424 0.000321987001111 Singular values of target I_tilde (truncating to first 4 of 6) = 3.0 1.0 1.0 1.0 2.46365884775e-16 9.80956846773e-17 Resulting gate set: rho0 = 0.6961 -0.0307 0.0425 0.7832 E0 = 0.7554 0.0111 -0.0141 -0.6685 Gi = 0.9997 -0.0005 0.0007 -0.0063 -0.0064 0.8998 0.0007 -0.0020 0.0037 -0.0023 0.8991 -0.0002 -0.0080 0.0006 0.0017 0.9024 Gx = 1.0223 0.0007 0.0476 -0.0050 -0.0039 0.8292 0.0241 -0.0040 0.0070 0.0263 -0.0403 -1.0042 -0.1197 0.0279 0.6526 0.0271 Gy = 1.0221 -0.0478 -0.0009 -0.0034 -0.0182 -0.0538 0.0319 1.0079 0.0003 0.0110 0.8295 -0.0148 -0.1260 -0.6490 -0.0304 0.0388 --- LGST --- Singular values of I_tilde (truncating to first 6 of 6) = 3.56525557364 0.755465956932 0.496239591347 0.487203220172 0.00691003018424 0.000321987001111 Singular values of target I_tilde (truncating to first 4 of 6) = 3.0 1.0 1.0 1.0 2.46365884775e-16 9.80956846773e-17 LGST: Padding target B with sqrt of low singular values of I_tilde: [ 0.00691003 0.00032199] Resulting gate set: rho0 = 0.6961 -0.0307 0.0425 0.7832 0.0374 -0.0007 E0 = 0.7554 0.0111 -0.0141 -0.6685 -0.0376 -0.0002 Gi = 0.9997 -0.0005 0.0007 -0.0063 -0.0015 -0.0020 -0.0064 0.8998 0.0007 -0.0020 -0.0171 -0.0049 0.0037 -0.0023 0.8991 -0.0002 0.0107 -0.0284 -0.0080 0.0006 0.0017 0.9024 -0.0006 -0.0612 -0.0013 0.0107 -0.0129 0.0197 0.9113 -0.3506 0.0073 -0.0197 0.0143 0.0980 0.2630 1.7004 Gx = 1.0223 0.0007 0.0476 -0.0050 0.0615 -0.0337 -0.0039 0.8292 0.0241 -0.0040 -0.0141 0.0412 0.0070 0.0263 -0.0403 -1.0042 0.3136 -0.0878 -0.1197 0.0279 0.6526 0.0271 -0.0017 -0.0003 -0.0748 -0.0036 -0.1573 0.0164 0.6755 -0.4246 0.0038 0.0568 0.0102 -0.0015 -0.2004 -0.2516 Gy = 1.0221 -0.0478 -0.0009 -0.0034 0.0676 -0.0495 -0.0182 -0.0538 0.0319 1.0079 -0.3170 0.0147 0.0003 0.0110 0.8295 -0.0148 0.0105 0.0349 -0.1260 -0.6490 -0.0304 0.0388 -0.0006 -0.0394 -0.0798 0.1549 -0.0002 0.0193 0.7669 -0.5002 -0.0245 -0.0182 0.0293 0.0340 -0.1267 -1.7344 LGST dim=4 chiSq = 1236262.37706 LGST dim=6 chiSq = 704616.980106 --- Iterative MC2GST: Iter 1 of 4 92 gate strings ---: --- Minimum Chi^2 GST with model selection (starting dim = 4) --- Dim 4: chi^2 = 278034, nGateStrings=92, nParams=56 (so expected mean = 36) Rejected dim 3: chi^2 = 3.17675e+06 (+3.17669e+06 w.r.t. expected mean of 92 strings - 33 params = 59) (dChi^2=2898715, 2*dParams=-46) Selected dim 5: chi^2 = 44.5422 (+37.5422 w.r.t. expected mean of 92 strings - 85 params = 7) (dChi^2=-277989, 2*dParams=58) --- Iterative MC2GST: Iter 2 of 4 488 gate strings ---: --- Minimum Chi^2 GST with model selection (starting dim = 5) --- Dim 5: chi^2 = 397.086, nGateStrings=488, nParams=85 (so expected mean = 403) Rejected dim 4: chi^2 = 1.39997e+06 (+1.39953e+06 w.r.t. expected mean of 488 strings - 56 params = 432) (dChi^2=1399568, 2*dParams=-58) Rejected dim 6: chi^2 = 340.934 (-27.0661 w.r.t. expected mean of 488 strings - 120 params = 368) (dChi^2=-56, 2*dParams=70) --- Iterative MC2GST: Iter 3 of 4 884 gate strings ---: --- Minimum Chi^2 GST with model selection (starting dim = 5) --- Dim 5: chi^2 = 802.805, nGateStrings=884, nParams=85 (so expected mean = 799) Rejected dim 4: chi^2 = 2.69456e+06 (+2.69373e+06 w.r.t. expected mean of 884 strings - 56 params = 828) (dChi^2=2693755, 2*dParams=-58) Rejected dim 6: chi^2 = 768.317 (+4.31666 w.r.t. expected mean of 884 strings - 120 params = 764) (dChi^2=-34, 2*dParams=70) --- Iterative MC2GST: Iter 4 of 4 1280 gate strings ---: --- Minimum Chi^2 GST with model selection (starting dim = 5) --- Dim 5: chi^2 = 1197.51, nGateStrings=1280, nParams=85 (so expected mean = 1195) Rejected dim 4: chi^2 = 4.21903e+06 (+4.2178e+06 w.r.t. expected mean of 1280 strings - 56 params = 1224) (dChi^2=4217830, 2*dParams=-58) Rejected dim 6: chi^2 = 1151.92 (-8.07686 w.r.t. expected mean of 1280 strings - 120 params = 1160) (dChi^2=-45, 2*dParams=70)
print(gs_lsgst)
rho0 = 0.7072 -0.0180 0.0258 0.7474 -0.0383 E0 = 0.7044 0.0387 -0.0460 -0.6514 0.2459 Gi = 1.0000 0 0 0 0 -0.0040 0.8993 0.0007 0.0013 -0.0067 0.0027 0.0004 0.8991 -0.0012 0.0083 -0.0088 -0.0002 0.0003 0.9002 -0.0004 -0.0002 0.0083 -0.0092 -0.0143 1.0004 Gx = 1.0000 0 0.0001 -0.0001 0 -0.0190 0.8319 0.0107 0.0002 0.0044 0.0274 0.0151 -0.0208 -1.0067 -0.0345 -0.1080 0.0315 0.6816 0.0313 -0.0499 0.1709 -0.0042 0.0254 -0.0139 0.7702 Gy = 1.0000 -0.0001 0 -0.0002 0 -0.0355 -0.0282 0.0228 1.0159 0.0442 0.0177 -0.0004 0.8313 -0.0176 -0.0058 -0.1141 -0.6751 -0.0352 0.0387 -0.0401 0.1706 -0.0331 0.0048 -0.0027 0.7708