Skip to content

Commit be477c3

Browse files
authored
Merge pull request #29 from qiskit-community/mixture_collapse
Add mixture collapse option and fix C++ instability
2 parents 1bd9e1d + d769974 commit be477c3

File tree

6 files changed

+59
-22
lines changed

6 files changed

+59
-22
lines changed

lindbladmpo/LindbladMPOSolver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ def verify_parameters(
928928
or (key == "b_save_final_state")
929929
or (key == "b_initial_rho_compression")
930930
or (key == "b_apply_gate_compression")
931+
or (key == "b_collapse_equal_mixture")
931932
):
932933
if not isinstance(parameters[key], bool):
933934
check_msg += (

lindbladmpo/examples/simulation_building/LindbladMatrixSolver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ def verify_parameters(
572572
"trotter_order",
573573
"b_initial_rho_compression",
574574
"b_apply_gate_compression",
575+
"b_collapse_equal_mixture",
575576
"max_dim_rho",
576577
"cut_off",
577578
"cut_off_rho",

src/ModelParameters.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class ModelParameters : public SimulationParameters
8080

8181
operator[]("custom_observables") = "";
8282
operator[]("collapse") = "";
83+
operator[]("b_collapse_equal_mixture") = "";
8384
}
8485
};
8586

src/lindbladian.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ bool SetLindbladian(SpinHalfSystem &C, ModelParameters param, Lattice2d L)
210210
g_2 = vector<double>(N, g_2[0]);
211211
if (g_3_len == 1)
212212
g_3 = vector<double>(N, g_3[0]);
213-
if (g_2_len == 1)
213+
if (g_4_len == 1)
214214
g_4 = vector<double>(N, g_4[0]);
215215

216216
for (int i = 1; i <= int(N); i++)

src/lindbladmpo.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ using namespace std::chrono;
2929

3030
stream2d cout2 = stream2d(&cerr, NULL);
3131

32-
const string SOLVER_VERSION = "0.3.1";
32+
const string SOLVER_VERSION = "0.3.2";
3333

3434
const double IMAGINARY_THRESHOLD = 1e-4;
3535
// Threshold for the imaginary value of a quantity that should be real, to issue a warning
@@ -870,10 +870,13 @@ int main(int argc, char *argv[])
870870
<< "\n";
871871
cout2.flush();
872872

873+
const bool b_collapse_equal_mixture = param.boolval("b_collapse_equal_mixture");
873874
if (collapse.size())
874875
{
875876
auto t_collapse_start = steady_clock::now();
876877
cout2 << "\nStarting evaluation of " << collapse.size() << " collapse operators.\n";
878+
if (b_collapse_equal_mixture)
879+
cout2 << "Creating an equal mixture of collapsed states.\n";
877880

878881
int i_coll = 0;
879882
MPS rho_0(C.rho); // Keep a copy of the uncollapsed state
@@ -902,6 +905,14 @@ int main(int argc, char *argv[])
902905
exit(1);
903906
i_op++;
904907
}
908+
if (b_collapse_equal_mixture)
909+
{
910+
Cplx z = C.trace_rho();
911+
if (std::abs(z) < _2_N)
912+
cout2 << "\t\tNote: " << "Tr{rho} = " << z << " encountered during collapse projectors, "
913+
<< "this is smaller than 2^(-N)!";
914+
C.rho /= z;
915+
}
905916
if (i_coll == 0)
906917
rho_c = MPS(C.rho);
907918
else

test/model/test_model.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -628,30 +628,49 @@ def test_apply_gates_2(self):
628628

629629
def test_apply_gates_cx_sx_h(self):
630630
"""Compare the application of gates at intermediate times."""
631-
N = 8
631+
N = 13
632632
t_final = 1
633633
solver_params1 = {
634-
"tau": 0.02,
634+
"tau": 0.1,
635635
"t_final": t_final,
636636
"N": N,
637-
"h_z": 0.43,
637+
"h_z": 0.0,
638638
"1q_components": ["x", "y", "z"],
639639
"2q_components": ["xx", "yy", "zz", "xy", "xz", "yz"],
640640
"b_quiet": True,
641-
"init_product_state": ["+x", "+x", "+z", "+z", "+z", "+z", "-z", "-z"],
641+
"init_product_state": [
642+
"+x",
643+
"+x",
644+
"+z",
645+
"+z",
646+
"+z",
647+
"+z",
648+
"-z",
649+
"-z",
650+
"+z",
651+
"+z",
652+
"+z",
653+
"+z",
654+
"+z",
655+
],
642656
"apply_gates": [
643-
(0.0, "cz", 0, 1),
644-
(0.0, "h", 2),
645-
(0.0, "h", 3),
646-
(0.0, "cz", 2, 3),
647-
(0.0, "h", 4),
648-
(0.0, "h", 5),
649-
(0.0, "h", 5),
650-
(0.0, "cx", 4, 5),
651-
(0.0, "h", 5),
652-
(0.0, "sx", 6),
653-
(0.0, "sx", 7),
654-
(0.0, "cz", 6, 7),
657+
(0.1, "cz", 0, 1),
658+
(0.2, "h", 2),
659+
(0.3, "h", 3),
660+
(0.4, "cz", 2, 3),
661+
(0.5, "h", 4),
662+
(0.6, "h", 5),
663+
(0.7, "h", 5),
664+
(0.8, "cx", 4, 5),
665+
(0.9, "h", 5),
666+
(0.3, "sx", 6),
667+
(0.4, "sx", 7),
668+
(0.5, "cz", 6, 7),
669+
(0.7, "h", 10),
670+
(0.7, "cx", 10, 8),
671+
(0.7, "h", 12),
672+
(0.8, "cx", 12, 11),
673+
(0.9, "cx", 8, 11),
655674
],
656675
}
657676
s_files_prefix1 = s_output_path + "test_apply_gates_cx_sx_h"
@@ -668,8 +687,8 @@ def test_apply_gates_cx_sx_h(self):
668687
solver1.result["obs-2q"][("xz", (2, 3))][1][-1],
669688
)
670689
self.assertAlmostEqual(
671-
solver1.result["obs-2q"][("xx", (0, 1))][1][-1],
672-
solver1.result["obs-2q"][("xx", (2, 3))][1][-1],
690+
solver1.result["obs-2q"][("yy", (0, 1))][1][-1],
691+
solver1.result["obs-2q"][("yy", (2, 3))][1][-1],
673692
)
674693
self.assertAlmostEqual(
675694
solver1.result["obs-2q"][("xy", (0, 1))][1][-1],
@@ -680,13 +699,17 @@ def test_apply_gates_cx_sx_h(self):
680699
solver1.result["obs-2q"][("xz", (4, 5))][1][-1],
681700
)
682701
self.assertAlmostEqual(
683-
solver1.result["obs-2q"][("xx", (0, 1))][1][-1],
684-
solver1.result["obs-2q"][("xx", (4, 5))][1][-1],
702+
solver1.result["obs-2q"][("yy", (0, 1))][1][-1],
703+
solver1.result["obs-2q"][("yy", (4, 5))][1][-1],
685704
)
686705
self.assertAlmostEqual(
687706
solver1.result["obs-2q"][("xy", (0, 1))][1][-1],
688707
solver1.result["obs-2q"][("xy", (4, 5))][1][-1],
689708
)
709+
self.assertAlmostEqual(
710+
solver1.result["global"][("s_2", ())][1][0],
711+
0.0,
712+
)
690713

691714
(_, c_data1), _ = prepare_concurrence_data(solver1.result, (0, 1))
692715
(_, c_data2), _ = prepare_concurrence_data(solver1.result, (6, 7))

0 commit comments

Comments
 (0)