Skip to content

Commit 25ee299

Browse files
committed
check in (needed for notebook committed in last commit to actually run
1 parent b789eeb commit 25ee299

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

wip_notebook_sharing/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This folder
2+
3+
Contents of this folder should be deleted before merge.
4+

wip_notebook_sharing/experiment_helpers.py

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pygsti.objectivefns import ObjectiveFunctionBuilder, ModelDatasetCircuitsStore
77
import pygsti.objectivefns
88
from pygsti.optimize import SimplerLMOptimizer
9+
import scipy.linalg as la
910

1011

1112
def make_depolarized_dataset(modelpack, depol_level=0.01, max_max_len=128):
@@ -23,21 +24,84 @@ def make_depolarized_dataset(modelpack, depol_level=0.01, max_max_len=128):
2324
return ds, depol_model
2425

2526

26-
def make_tweaked_dataset(modelpack, depol_level=0.01, rand_unitary_scale=0.001, max_max_len=128):
27+
def make_tweaked_dataset(modelpack, depol_level=0.01, rand_unitary_scale=0.001, max_max_len=128, sample_error='multinomial', seed=0, shots_per_circuit=1000):
2728
ideal_model = modelpack.target_model()
2829
prep_fids = modelpack.prep_fiducials()
2930
meas_fids = modelpack.meas_fiducials()
3031
germs = modelpack.germs()
3132
max_lens = [2**p for p in range(1+int(np.log2(max_max_len)))]
3233
lsgst_circuit_lists = pygsti.circuits.create_lsgst_circuit_lists(ideal_model, prep_fids, meas_fids, germs, max_lens)
3334
all_circuits = lsgst_circuit_lists[-1]
34-
shots_per_circuit = 1000
35-
depol_model = ideal_model.depolarize(op_noise=depol_level, spam_noise=depol_level/2, seed=1997)
36-
final_model = depol_model.randomize_with_unitary(scale=rand_unitary_scale, seed=250422)
37-
rng_state = np.random.default_rng(0)
38-
ds = pygsti.data.simulate_data(final_model, all_circuits, shots_per_circuit, rand_state=rng_state)
35+
36+
depol_model = ideal_model.depolarize(op_noise=depol_level, spam_noise=depol_level/2, seed=seed+1997)
37+
final_model = depol_model.randomize_with_unitary(scale=rand_unitary_scale, seed=seed+250422, transform_spam=True)
38+
rng_state = np.random.default_rng(seed)
39+
ds = pygsti.data.simulate_data(final_model, all_circuits, shots_per_circuit, sample_error=sample_error, rand_state=rng_state)
40+
3941
return ds, final_model
4042

43+
def make_tweaked_dataset_pairs(modelpack, depol_level=0.01, rand_unitary_scale=0.001, max_max_len=128, sample_error='multinomial', seed=0, shots_per_circuit=1000, gaugeopt=True):
44+
ideal_model = modelpack.target_model()
45+
prep_fids = modelpack.prep_fiducials()
46+
meas_fids = modelpack.meas_fiducials()
47+
germs = modelpack.germs()
48+
max_lens = [2**p for p in range(1+int(np.log2(max_max_len)))]
49+
lsgst_circuit_lists = pygsti.circuits.create_lsgst_circuit_lists(ideal_model, prep_fids, meas_fids, germs, max_lens)
50+
all_circuits = lsgst_circuit_lists[-1]
51+
52+
depol_model = ideal_model.depolarize(op_noise=depol_level, spam_noise=depol_level/2, seed=seed+1997)
53+
54+
model_a = depol_model.copy()
55+
model_b = depol_model.copy()
56+
rndm = np.random.RandomState(seed+250422)
57+
import pygsti.tools.optools as _ot
58+
import pygsti.modelmembers.operations as _op
59+
from pygsti.modelmembers.povms import create_from_dmvecs
60+
from pygsti.modelmembers.states import FullState
61+
unitary_dim = 2
62+
basis = depol_model.basis
63+
64+
def rand_unitary_as_superop():
65+
rand_mat = rndm.randn(unitary_dim, unitary_dim) + 1j * rndm.randn(unitary_dim, unitary_dim)
66+
rand_herm = rand_mat.T.conj() + rand_mat
67+
rand_herm /= la.norm(rand_herm)
68+
rand_herm *= rand_unitary_scale * np.sqrt(unitary_dim)
69+
rand_unitary = la.expm(-1j * rand_herm)
70+
rand_op = _ot.unitary_to_superop(rand_unitary, basis)
71+
assert la.norm(rand_op @ rand_op.T - np.eye(unitary_dim**2)) < 1e-12
72+
return rand_op
73+
74+
for opLabel, gate in depol_model.operations.items():
75+
rand_op = rand_unitary_as_superop()
76+
model_a.operations[opLabel] = _op.FullArbitraryOp(rand_op @ gate)
77+
model_b.operations[opLabel] = _op.FullArbitraryOp(rand_op.T @ gate)
78+
79+
for preplbl, rho in depol_model.preps.items():
80+
rand_op = rand_unitary_as_superop()
81+
model_a.preps[preplbl] = FullState(rand_op @ rho)
82+
model_b.preps[preplbl] = FullState(rand_op.T @ rho)
83+
84+
for povmlbl, M in depol_model.povms.items():
85+
rand_op = rand_unitary_as_superop()
86+
dmvecs = {elbl: rand_op @ e.to_dense() for elbl, e in M.items()}
87+
model_a.povms[povmlbl] = create_from_dmvecs(dmvecs, 'full')
88+
dmvecs = {elbl: rand_op.T @ e.to_dense() for elbl, e in M.items()}
89+
model_b.povms[povmlbl] = create_from_dmvecs(dmvecs, 'full')
90+
91+
rng_state = np.random.default_rng(seed)
92+
dsa = pygsti.data.simulate_data(model_a, all_circuits, shots_per_circuit, sample_error=sample_error, rand_state=rng_state)
93+
dsb = pygsti.data.simulate_data(model_b, all_circuits, shots_per_circuit, sample_error=sample_error, rand_state=rng_state)
94+
95+
if gaugeopt:
96+
from pygsti.algorithms.gaugeopt import gaugeopt_to_target
97+
for model in [model_a, model_b]:
98+
model.convert_members_inplace('full')
99+
model.default_gauge_group = 'unitary'
100+
model_a = gaugeopt_to_target(model_a, ideal_model)
101+
model_b = gaugeopt_to_target(model_b, ideal_model)
102+
103+
return dsa, model_a, dsb, model_b
104+
41105

42106

43107
def corrupt_dataset(ds, prop_corrupt, rng=0):

0 commit comments

Comments
 (0)