def test_simple(self): p = neel_state(1) assert_allclose(p, up()) p = neel_state(2) assert_allclose(p, up() & down()) p = neel_state(3) assert_allclose(p, up() & down() & up())
def test_simple(self): Z = qu.pauli('Z') P = qu.projector(Z & Z) uu = qu.dop(qu.up()) & qu.dop(qu.up()) dd = qu.dop(qu.down()) & qu.dop(qu.down()) assert_allclose(P, uu + dd) assert qu.expec(P, qu.bell_state('phi+')) == pytest.approx(1.0) assert qu.expec(P, qu.bell_state('psi+')) == pytest.approx(0.0)
def test_classically_correlated(self, s, ct, pre_c): p = 0.5 * ((qu.up(qtype='dop') & qu.up(qtype='dop')) + (qu.down(qtype='dop') & qu.down(qtype='dop'))) c = qu.correlation(p, qu.pauli(s), qu.pauli(s), 0, 1, precomp_func=pre_c) c = c(p) if pre_c else c assert_allclose(c, ct)
def test_quevo_multi_compute(self, method, qtype): ham = ham_heis(2, cyclic=False) p0 = qu(up() & down(), qtype=qtype) def some_quantity(t, _): return t def some_other_quantity(_, pt): return logneg(pt) evo = QuEvo(p0, ham, method=method, compute={ 't': some_quantity, 'logneg': some_other_quantity }) manual_lns = [] for pt in evo.at_times(np.linspace(0, 1, 6)): manual_lns.append(logneg(pt)) ts = evo.results['t'] lns = evo.results['logneg'] assert len(lns) >= len(manual_lns) # check a specific value of logneg at t=0.8 was computed automatically checked = False for t, ln in zip(ts, lns): if abs(t - 0.8) < 1e-12: assert abs(ln - manual_lns[4]) < 1e-12 checked = True assert checked
def test_bell_state(self): p = qu.bell_state('psi-') assert_allclose(qu.schmidt_gap(p, [2, 2], 0), 0.0) p = qu.up() & qu.down() assert_allclose(qu.schmidt_gap(p, [2, 2], 0), 1.0) p = qu.rand_ket(2**3) assert 0 < qu.schmidt_gap(p, [2] * 3, sysa=[0, 1]) < 1.0
def test_bell_state(self): p = bell_state('psi-') assert_allclose(schmidt_gap(p, [2, 2], 0), 0.0) p = up() & down() assert_allclose(schmidt_gap(p, [2, 2], 0), 1.0) p = rand_ket(2**3) assert 0 < schmidt_gap(p, [2] * 3, sysa=[0, 1]) < 1.0
def test_progbar_update_to_integrate(self, capsys): ham = ham_heis(2, cyclic=False) p0 = up() & down() sim = QuEvo(p0, ham, method='integrate', progbar=True) sim.update_to(100) # check something as been printed _, err = capsys.readouterr() assert err and "%" in err
def test_progbar_at_times_expm(self, capsys): ham = ham_heis(2, cyclic=False) p0 = up() & down() sim = QuEvo(p0, ham, method='expm', progbar=True) for _ in sim.at_times(np.linspace(0, 100, 11)): pass # check something as been printed _, err = capsys.readouterr() assert err and "%" in err
def test_quevo_at_times(self): ham = ham_heis(2, cyclic=False) p0 = up() & down() sim = QuEvo(p0, ham, method='solve') ts = np.linspace(0, 10) for t, pt in zip(ts, sim.at_times(ts)): x = cos(t) y = expec(pt, eyepad(pauli('z'), [2, 2], 0)) assert_allclose(x, y, atol=1e-15)
def test_progbar_at_times_solve(self, capsys): ham = qu.ham_heis(2, cyclic=False) p0 = qu.up() & qu.down() sim = qu.Evolution(p0, ham, method='solve', progbar=True) for _ in sim.at_times(np.linspace(0, 100, 11)): pass # check something as been printed _, err = capsys.readouterr() assert err and "%" in err
def test_evo_at_times(self): ham = qu.ham_heis(2, cyclic=False) p0 = qu.up() & qu.down() sim = qu.Evolution(p0, ham, method='solve') ts = np.linspace(0, 10) for t, pt in zip(ts, sim.at_times(ts)): x = cos(t) y = qu.expec(pt, qu.ikron(qu.pauli('z'), [2, 2], 0)) assert_allclose(x, y, atol=1e-15)
class TestDecomp: @pytest.mark.parametrize("qtype", ['ket', 'dop']) def test_pauli_decomp_singlet(self, qtype): p = qu.singlet(qtype=qtype) names_cffs = qu.pauli_decomp(p, mode='cp') assert_allclose(names_cffs['II'], 0.25) assert_allclose(names_cffs['ZZ'], -0.25) assert_allclose(names_cffs['YY'], -0.25) assert_allclose(names_cffs['ZZ'], -0.25) for name in itertools.permutations('IXYZ', 2): assert_allclose(names_cffs["".join(name)], 0.0) def test_pauli_reconstruct(self): p1 = qu.rand_rho(4) names_cffs = qu.pauli_decomp(p1, mode='c') pr = sum( qu.kron(*(qu.pauli(s) for s in name)) * names_cffs["".join(name)] for name in itertools.product('IXYZ', repeat=2)) assert_allclose(pr, p1) @pytest.mark.parametrize("state, out", [(qu.up() & qu.down(), { 0: 0.5, 1: 0.5, 2: 0, 3: 0 }), (qu.down() & qu.down(), { 0: 0, 1: 0, 2: 0.5, 3: 0.5 }), (qu.singlet() & qu.singlet(), { '00': 1.0, '23': 0.0 })]) def test_bell_decomp(self, state, out): names_cffs = qu.bell_decomp(state, mode='c') for key in out: assert_allclose(names_cffs[str(key)], out[key])
def test_quevo_compute_callback(self, qtype, method): ham = ham_heis(2, cyclic=False) p0 = qu(up() & down(), qtype=qtype) def some_quantity(t, pt): return t, logneg(pt) evo = QuEvo(p0, ham, method=method, compute=some_quantity) manual_lns = [] for pt in evo.at_times(np.linspace(0, 1, 6)): manual_lns.append(logneg(pt)) ts, lns = zip(*evo.results) assert len(lns) >= len(manual_lns) # check a specific value of logneg at t=0.8 was computed automatically checked = False for t, ln in zip(ts, lns): if abs(t - 0.8) < 1e-12: assert abs(ln - manual_lns[4]) < 1e-12 checked = True assert checked
def test_evo_multi_compute(self, method, qtype): ham = qu.ham_heis(2, cyclic=False) p0 = qu.qu(qu.up() & qu.down(), qtype=qtype) def some_quantity(t, _): return t def some_other_quantity(_, pt): return qu.logneg(pt) # check that hamiltonian gets accepted without error for all methods def some_other_quantity_accepting_ham(t, pt, H): return qu.logneg(pt) compute = { 't': some_quantity, 'logneg': some_other_quantity, 'logneg_ham': some_other_quantity_accepting_ham } evo = qu.Evolution(p0, ham, method=method, compute=compute) manual_lns = [] for pt in evo.at_times(np.linspace(0, 1, 6)): manual_lns.append(qu.logneg(pt)) ts = evo.results['t'] lns = evo.results['logneg'] lns_ham = evo.results['logneg_ham'] assert len(lns) >= len(manual_lns) # check a specific value of logneg at t=0.8 was computed automatically checked = False for t, ln, ln_ham in zip(ts, lns, lns_ham): if abs(t - 0.8) < 1e-12: assert abs(ln - manual_lns[4]) < 1e-12 # check that accepting hamiltonian didn't mess it up assert ln == ln_ham checked = True assert checked
def test_distinguishable(self, uqtype, dqtype): assert qu.trace_distance(qu.up(qtype=uqtype), qu.down(qtype=dqtype)) > 1 - 1e-10
def test_swap_qubits(self, sparse): a = up() & down() s = swap(2, sparse=sparse) assert_allclose(s @ a, down() & up())
def test_down(self): p = down(qtype='dop') assert_allclose(tr(p @ pauli('z')), -1.0)
def test_n2(self): p = perm_state([up(), down()]) assert_allclose(p, bell_state('psi-'))
def test_swap_qubits(self, sparse): a = qu.up() & qu.down() s = qu.swap(2, sparse=sparse) assert_allclose(s @ a, qu.down() & qu.up())
def test_classically_correlated(self, dir, ct, pre_c): p = 0.5 * ((up(qtype='dop') & up(qtype='dop')) + (down(qtype='dop') & down(qtype='dop'))) c = correlation(p, pauli(dir), pauli(dir), 0, 1, precomp_func=pre_c) c = c(p) if pre_c else c assert_allclose(c, ct)