def test_access_tableau(): s = stim.TableauSimulator() assert s.current_inverse_tableau() == stim.Tableau(0) s.h(0) assert s.current_inverse_tableau() == stim.Tableau.from_named_gate("H") s.h(0) assert s.current_inverse_tableau() == stim.Tableau(1) s.h(1) s.h(1) assert s.current_inverse_tableau() == stim.Tableau(2) s.h(2) assert s.current_inverse_tableau( ) == stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X__"), stim.PauliString("_X_"), stim.PauliString("__Z"), ], zs=[ stim.PauliString("Z__"), stim.PauliString("_Z_"), stim.PauliString("__X"), ], )
def test_pow(): s = stim.Tableau.from_named_gate("S") s_dag = stim.Tableau.from_named_gate("S_DAG") z = stim.Tableau.from_named_gate("Z") assert stim.Tableau(1) == s**0 == s**4 == s**-4 assert s == s**1 == s**5 == s**-3 == s**(40000 + 1) == s**(-40000 + 1) assert s_dag == s**-1 == s**3 == s**7 == s**(40000 + 3) == s**(-40000 + 3) assert z == s**2 == s**6 == s**-2 == s**(40000 + 2) == s**(-40000 + 2)
def test_identity(): t = stim.Tableau(3) assert len(t) == 3 assert t.x_output(0) == stim.PauliString("X__") assert t.x_output(1) == stim.PauliString("_X_") assert t.x_output(2) == stim.PauliString("__X") assert t.z_output(0) == stim.PauliString("Z__") assert t.z_output(1) == stim.PauliString("_Z_") assert t.z_output(2) == stim.PauliString("__Z")
def test_prepend(): t = stim.Tableau(2) with pytest.raises(ValueError, match=re.escape("len(targets) != len(gate)")): t.prepend(stim.Tableau.from_named_gate("CY"), [0]) with pytest.raises(ValueError, match="collision"): t.prepend(stim.Tableau.from_named_gate("CY"), [0, 0]) with pytest.raises(ValueError, match=re.escape("target >= len(tableau)")): t.prepend(stim.Tableau.from_named_gate("CY"), [1, 2]) t.prepend(stim.Tableau.from_named_gate("SQRT_X_DAG"), [1]) t.prepend(stim.Tableau.from_named_gate("CY"), [0, 1]) t.prepend(stim.Tableau.from_named_gate("SQRT_X"), [1]) assert t != stim.Tableau.from_named_gate("CZ") t = stim.Tableau(2) t.prepend(stim.Tableau.from_named_gate("SQRT_X"), [1]) t.prepend(stim.Tableau.from_named_gate("CY"), [0, 1]) t.prepend(stim.Tableau.from_named_gate("SQRT_X_DAG"), [1]) assert t == stim.Tableau.from_named_gate("CZ")
def test_init_equality(): assert stim.Tableau(3) == stim.Tableau(3) assert not (stim.Tableau(3) != stim.Tableau(3)) assert stim.Tableau(3) != stim.Tableau(4) assert not (stim.Tableau(3) == stim.Tableau(4)) assert stim.Tableau.from_named_gate("S") == stim.Tableau.from_named_gate( "S") assert stim.Tableau.from_named_gate("S") != stim.Tableau.from_named_gate( "S_DAG") assert stim.Tableau.from_named_gate("S") != stim.Tableau.from_named_gate( "H") assert stim.Tableau.from_named_gate( "S_DAG") == stim.Tableau.from_named_gate("S_DAG")
def test_add(): h = stim.Tableau.from_named_gate("H") swap = stim.Tableau.from_named_gate("SWAP") cnot = stim.Tableau.from_named_gate("CNOT") combo = h + swap + cnot assert str(combo).strip() == """ +-xz-xz-xz-xz-xz- | ++ ++ ++ ++ ++ | ZX __ __ __ __ | __ __ XZ __ __ | __ XZ __ __ __ | __ __ __ XZ _Z | __ __ __ X_ XZ """.strip() alias = h h += swap h += cnot assert h == combo h += stim.Tableau(0) assert h == combo assert h is alias assert h is not combo assert swap == stim.Tableau.from_named_gate("SWAP") assert stim.Tableau(0) + stim.Tableau(0) == stim.Tableau(0) assert stim.Tableau(1) + stim.Tableau(2) == stim.Tableau(3) assert stim.Tableau(100) + stim.Tableau(500) == stim.Tableau(600) assert stim.Tableau(0) + cnot + stim.Tableau(0) == cnot x = stim.Tableau.from_named_gate("X") y = stim.Tableau.from_named_gate("Y") z = stim.Tableau.from_named_gate("Z") assert str(y + y).strip() == """ +-xz-xz- | -- -- | XZ __ | __ XZ """.strip() assert str(x + x).strip() == """ +-xz-xz- | +- +- | XZ __ | __ XZ """.strip() assert str(z + z).strip() == """ +-xz-xz- | -+ -+ | XZ __ | __ XZ """.strip() assert str(x + z).strip() == """ +-xz-xz- | +- -+ | XZ __ | __ XZ """.strip() assert str(z + x).strip() == """ +-xz-xz- | -+ +- | XZ __ | __ XZ """.strip()
def test_hash(): # stim.Tableau is mutable. It must not also be value-hashable. # Defining __hash__ requires defining a FrozenTableau variant instead. with pytest.raises(TypeError, match="unhashable"): _ = hash(stim.Tableau(1))
def test_copy(): t = stim.Tableau(3) t2 = t.copy() assert t == t2 assert t is not t2
def test_composition(): assert stim.Tableau(0) * stim.Tableau(0) == stim.Tableau(0) assert stim.Tableau(0).then(stim.Tableau(0)) == stim.Tableau(0) assert stim.Tableau(1) * stim.Tableau(1) == stim.Tableau(1) assert stim.Tableau(1).then(stim.Tableau(1)) == stim.Tableau(1) t = stim.Tableau.random(4) t2 = stim.Tableau.random(4) t3 = t.then(t2) assert t3 == t2 * t p = stim.PauliString.random(4) assert t2(t(p)) == t3(p) with pytest.raises(ValueError, match="!= len"): _ = stim.Tableau(3) * stim.Tableau(4) with pytest.raises(ValueError, match="!= len"): _ = stim.Tableau(3).then(stim.Tableau(4))
def test_init_equality(): assert stim.Tableau(3) != None assert stim.Tableau(3) != object() assert stim.Tableau(3) != "another type" assert not (stim.Tableau(3) == None) assert not (stim.Tableau(3) == object()) assert not (stim.Tableau(3) == "another type") assert stim.Tableau(3) == stim.Tableau(3) assert not (stim.Tableau(3) != stim.Tableau(3)) assert stim.Tableau(3) != stim.Tableau(4) assert not (stim.Tableau(3) == stim.Tableau(4)) assert stim.Tableau.from_named_gate("S") == stim.Tableau.from_named_gate( "S") assert stim.Tableau.from_named_gate("S") != stim.Tableau.from_named_gate( "S_DAG") assert stim.Tableau.from_named_gate("S") != stim.Tableau.from_named_gate( "H") assert stim.Tableau.from_named_gate( "S_DAG") == stim.Tableau.from_named_gate("S_DAG")
def test_from_conjugated_generators(): assert stim.Tableau(3) == stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X__"), stim.PauliString("_X_"), stim.PauliString("__X"), ], zs=[ stim.PauliString("Z__"), stim.PauliString("_Z_"), stim.PauliString("__Z"), ], ) assert stim.Tableau.from_named_gate( "S") == stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("Y"), ], zs=[ stim.PauliString("Z"), ], ) assert stim.Tableau.from_named_gate( "S_DAG") == stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("-Y"), ], zs=[ stim.PauliString("Z"), ], ) assert stim.Tableau(2) == stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X_"), stim.PauliString("_X"), ], zs=[ stim.PauliString("Z_"), stim.PauliString("_Z"), ], ) with pytest.raises(ValueError, match=re.escape("len(p) == len(zs)")): stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X_"), stim.PauliString("_X"), ], zs=[ stim.PauliString("Z_"), stim.PauliString("_Z_"), ], ) with pytest.raises(ValueError, match="imag"): stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("iX_"), stim.PauliString("_X"), ], zs=[ stim.PauliString("Z_"), stim.PauliString("_Z"), ], ) with pytest.raises(ValueError, match="imag"): stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X_"), stim.PauliString("_X"), ], zs=[ stim.PauliString("Z_"), stim.PauliString("i_Z"), ], ) with pytest.raises(ValueError, match=re.escape("len(p) == len(xs)")): stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X_"), stim.PauliString("_X_"), ], zs=[ stim.PauliString("Z_"), stim.PauliString("_Z"), ], ) with pytest.raises(ValueError, match=re.escape("len(xs) != len(zs)")): stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X_"), ], zs=[ stim.PauliString("Z_"), stim.PauliString("_Z"), ], ) with pytest.raises(ValueError, match=re.escape("len(xs) != len(zs)")): stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X_"), stim.PauliString("_X"), ], zs=[ stim.PauliString("Z_"), ], ) with pytest.raises(ValueError, match="commutativity"): stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X_"), stim.PauliString("_Z"), ], zs=[ stim.PauliString("Z_"), stim.PauliString("_Z"), ], ) with pytest.raises(ValueError, match="commutativity"): stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X_"), stim.PauliString("Z_"), ], zs=[ stim.PauliString("Z_"), stim.PauliString("X_"), ], ) with pytest.raises(ValueError, match="commutativity"): stim.Tableau.from_conjugated_generators( xs=[ stim.PauliString("X_"), stim.PauliString("X_"), ], zs=[ stim.PauliString("Z_"), stim.PauliString("Z_"), ], )