def test_inverse(): # Random circuit circ = qf.Circuit() circ += qf.TY(1 / 2, 0) circ += qf.H(0) circ += qf.TY(1 / 2, 1) circ += qf.TX(1.23123, 1) circ += qf.CNOT(0, 1) circ += qf.TX(-1 / 2, 1) circ += qf.TY(4.71572463191 / pi, 1) circ += qf.CNOT(0, 1) circ += qf.TX(-2 * 2.74973750579 / pi, 0) circ += qf.TX(-2 * 2.74973750579 / pi, 1) circ_inv = circ.H ket = circ.run() qf.print_state(ket) ket = circ_inv.run(ket) qf.print_state(ket) print(ket.qubits) print(true_ket().qubits) assert qf.states_close(ket, qf.zero_state(2)) ket = qf.zero_state(2) circ.extend(circ_inv) ket = circ.run(ket) assert qf.states_close(ket, qf.zero_state(2))
def test_CH(): # Construct a controlled Hadamard gate gate = qf.identity_gate(2) gate = qf.S(1).H @ gate gate = qf.H(1) @ gate gate = qf.T(1).H @ gate gate = qf.CNOT(0, 1) @ gate gate = qf.T(1) @ gate gate = qf.H(1) @ gate gate = qf.S(1) @ gate # Do nothing ket = qf.zero_state(2) ket = gate.run(ket) assert qf.states_close(ket, qf.zero_state(2)) # Do nothing ket = qf.zero_state(2) ket = qf.X(0).run(ket) ket = gate.run(ket) ket = qf.H(1).run(ket) ket = qf.X(0).run(ket) assert qf.states_close(ket, qf.zero_state(2))
def test_inverse() -> None: # Random circuit circ = qf.Circuit() circ += qf.YPow(1 / 2, 0) circ += qf.H(0) circ += qf.YPow(1 / 2, 1) circ += qf.XPow(1.23123, 1) circ += qf.CNot(0, 1) circ += qf.XPow(-1 / 2, 1) circ += qf.YPow(4.71572463191 / np.pi, 1) circ += qf.CNot(0, 1) circ += qf.XPow(-2 * 2.74973750579 / np.pi, 0) circ += qf.XPow(-2 * 2.74973750579 / np.pi, 1) circ_inv = circ.H ket = circ.run() # qf.print_state(ket) ket = circ_inv.run(ket) # qf.print_state(ket) # print(ket.qubits) # print(true_ket().qubits) assert qf.states_close(ket, qf.zero_state(2)) ket = qf.zero_state(2) circ += circ_inv ket = circ.run(ket) assert qf.states_close(ket, qf.zero_state(2))
def test_join_gates(): gate = qf.join_gates(qf.H(0), qf.X(1)) ket = qf.zero_state(2) ket = gate.run(ket) ket = qf.H(0).run(ket) ket = qf.X(1).run(ket) assert qf.states_close(ket, qf.zero_state(2))
def test_outer_product(): with pytest.raises(ValueError): ket = qf.zero_state(1) gate = qf.I(2) qf.outer_product(ket.vec, gate.vec) with pytest.raises(ValueError): ket0 = qf.zero_state([0, 1, 2]) ket1 = qf.zero_state([2, 3, 4]) qf.outer_product(ket0.vec, ket1.vec)
def test_create(): gen = [qf.H(i) for i in range(8)] circ1 = qf.Circuit(list(gen)) circ1.run(qf.zero_state(8)) circ2 = qf.Circuit(gen) circ2.run(qf.zero_state(8)) circ3 = qf.Circuit(qf.H(i) for i in range(8)) circ3.run(qf.zero_state(8))
def test_join_states() -> None: q0 = qf.zero_state([0]) q1 = qf.zero_state([1, 2]) q2 = qf.join_states(q0, q1) assert q2.qubit_nb == 3 assert q2.tensor[0, 0, 0] == 1 q3 = qf.zero_state([3, 4, 5]) q3 = qf.X(4).run(q3) q4 = qf.join_states(qf.join_states(q0, q1), q3) assert q4.qubit_nb == 6 assert q4.tensor[0, 0, 0, 0, 1, 0] == 1
def test_gradients() -> None: # This test only checks that code runs, not that we get correct answers # graph = nx.grid_graph([2, 3]) graph = nx.grid_graph([2, 1]) layers = 2 params = qf.graph_circuit_params(graph, layers) circ = qf.graph_circuit(graph, layers, params) circ += qf.H((1, 1)) # add a non-parameterized gate. Should be ignored qubits = circ.qubits ket0 = qf.zero_state(qubits) ket1 = qf.random_state(qubits) grads0 = qf.state_fidelity_gradients(ket0, ket1, circ) # print(grads0) _ = qf.state_angle_gradients(ket0, ket1, circ) # print(grads1) proj = qf.Projection([ket1]) grads2 = qf.expectation_gradients(ket0, circ, hermitian=proj) # print(grads2) # Check that qf.expectation_gradients() gives same answers for # fidelity as f.state_fidelity_gradients() for g0, g1 in zip(grads0, grads2): assert np.isclose(g0, g1) print(g0, g1)
def test_biased_coin(): # sample from a 75% head and 25% tails coin rho = qf.zero_state(1).asdensity() chan = qf.RX(np.pi / 3, 0).aschannel() rho = chan.evolve(rho) prob = qf.asarray(rho.probabilities()) assert np.allclose(prob, [0.75, 0.25])
def test_run() -> None: x = sX(1) y = sY(2, 1.2) s = x + y ket0 = qf.zero_state(3) _ = s.run(ket0)
def test_fubini_study_angle_states() -> None: # The state angle is half angle in Bloch sphere angle1 = 0.1324 ket1 = qf.zero_state(1) ket2 = qf.Rx(angle1, 0).run(ket1) angle2 = qf.fubini_study_angle(ket1.tensor, ket2.tensor) assert np.isclose(angle1, angle2 * 2)
def test_inner_product(): # also tested via test_gate_angle for _ in range(REPS): theta = random.uniform(-4 * pi, +4 * pi) hs = qf.asarray(qf.inner_product(qf.RX(theta).vec, qf.RX(theta).vec)) print('RX({}), hilbert_schmidt = {}'.format(theta, hs)) assert hs / 2 == ALMOST_ONE hs = qf.asarray(qf.inner_product(qf.RZ(theta).vec, qf.RZ(theta).vec)) print('RZ({}), hilbert_schmidt = {}'.format(theta, hs)) assert hs / 2 == ALMOST_ONE hs = qf.asarray(qf.inner_product(qf.RY(theta).vec, qf.RY(theta).vec)) print('RY({}), hilbert_schmidt = {}'.format(theta, hs)) assert hs / 2 == ALMOST_ONE hs = qf.asarray( qf.inner_product(qf.PSWAP(theta).vec, qf.PSWAP(theta).vec)) print('PSWAP({}), hilbert_schmidt = {}'.format(theta, hs)) assert hs / 4 == ALMOST_ONE with pytest.raises(ValueError): qf.inner_product(qf.zero_state(0).vec, qf.X(0).vec) with pytest.raises(ValueError): qf.inner_product(qf.CNOT(0, 1).vec, qf.X(0).vec)
def test_qftgate() -> None: circ = qf.Circuit() circ += qf.X(2) circ += qf.QFTGate([0, 1, 2]) ket = qf.zero_state(3) ket = circ.run(ket) true_qft = qf.State( [ 0.35355339 + 0.0j, 0.25000000 + 0.25j, 0.00000000 + 0.35355339j, -0.25000000 + 0.25j, -0.35355339 + 0.0j, -0.25000000 - 0.25j, 0.00000000 - 0.35355339j, 0.25000000 - 0.25j, ] ) assert qf.states_close(ket, true_qft) assert isinstance(qf.QFTGate([0, 1, 2]).H, qf.InvQFTGate) assert isinstance(qf.QFTGate([0, 1, 2]).H.H, qf.QFTGate) qf.QFTGate([0, 1, 2]).H.tensor
def test_fubini_study_angle_states(): # The state angle is half angle in Bloch sphere angle1 = 0.1324 ket1 = qf.zero_state(1) ket2 = qf.RX(angle1, 0).run(ket1) angle2 = qf.asarray(qf.fubini_study_angle(ket1.vec, ket2.vec)) assert angle1 - angle2 * 2 == ALMOST_ZERO
def test_inner_product() -> None: # also tested via fubini_study_angle for _ in range(REPS): theta = random.uniform(-4 * pi, +4 * pi) hs = tensors.inner(qf.Rx(theta, 0).tensor, qf.Rx(theta, 0).tensor) print(f"Rx({theta}), hilbert_schmidt = {hs}") assert np.isclose(hs / 2, 1.0) hs = tensors.inner(qf.Rz(theta, 0).tensor, qf.Rz(theta, 0).tensor) print(f"Rz({theta}), hilbert_schmidt = {hs}") assert np.isclose(hs / 2, 1.0) hs = tensors.inner(qf.Ry(theta, 0).tensor, qf.Ry(theta, 0).tensor) print(f"Ry({theta}), hilbert_schmidt = {hs}") assert np.isclose(hs / 2, 1.0) hs = tensors.inner( qf.PSwap(theta, 0, 1).tensor, qf.PSwap(theta, 0, 1).tensor) print(f"PSwap({theta}), hilbert_schmidt = {hs}") assert np.isclose(hs / 4, 1.0) with pytest.raises(ValueError): tensors.inner(qf.zero_state(0).tensor, qf.X(0).tensor) with pytest.raises(ValueError): tensors.inner(qf.CNot(0, 1).tensor, qf.X(0).tensor)
def test_sample() -> None: ket = qf.zero_state(2) ket = qf.H(0).run(ket) ket = qf.CNot(0, 1).run(ket) samples = ket.sample(10) assert samples.sum() == 10
def test_expectation() -> None: ket = qf.zero_state(1) ket = qf.H(0).run(ket) m = ket.expectation([0.4, 0.6]) assert np.isclose(m, 0.5) ket.expectation([0.4, 0.6], 10)
def test_expectation(): ket = qf.zero_state(1) ket = qf.H(0).run(ket) m = ket.expectation([0.4, 0.6]) assert qf.asarray(m) - 0.5 == ALMOST_ZERO m = ket.expectation([0.4, 0.6], 10)
def test_join_states(): q0 = qf.zero_state([0]) q1 = qf.zero_state([1, 2]) q2 = qf.join_states(q0, q1) assert q2.qubit_nb == 3 assert q2.vec.asarray()[0, 0, 0] == ALMOST_ONE q3 = qf.zero_state([3, 4, 5]) q3 = qf.X(4).run(q3) print(q0) print(q1) print(q3) q4 = qf.join_states(qf.join_states(q0, q1), q3) assert q4.qubit_nb == 6 assert q4.vec.asarray()[0, 0, 0, 0, 1, 0] == ALMOST_ONE
def test_CZ() -> None: ket = qf.zero_state(2) ket = qf.CZ(0, 1).run(ket) assert ket.tensor[0, 0] == 1.0 ket = qf.X(0).run(ket) ket = qf.X(1).run(ket) ket = qf.CZ(0, 1).run(ket) assert -ket.tensor[1, 1] == 1.0
def test_bits() -> None: ket = qf.X(0).run(qf.zero_state(4)) assert ket.tensor[1, 0, 0, 0] == 1 ket = qf.X(1).run(qf.zero_state(4)) assert ket.tensor[0, 1, 0, 0] == 1 ket = qf.X(2).run(qf.zero_state(4)) assert ket.tensor[0, 0, 1, 0] == 1 ket = qf.X(3).run(qf.zero_state(4)) assert ket.tensor[0, 0, 0, 1] == 1 ket = qf.zero_state(8) ket = qf.X(2).run(ket) ket = qf.X(4).run(ket) ket = qf.X(6).run(ket) res = ket.tensor assert res[0, 0, 1, 0, 1, 0, 1, 0]
def test_CZ(): ket = qf.zero_state(2) ket = qf.CZ(0, 1).run(ket) assert ket.vec.asarray()[0, 0] == ALMOST_ONE ket = qf.X(0).run(ket) ket = qf.X(1).run(ket) ket = qf.CZ(0, 1).run(ket) assert -ket.vec.asarray()[1, 1] == ALMOST_ONE
def test_sample_bell() -> None: rho = qf.zero_state(2).asdensity() chan = qf.H(0).aschannel() rho = chan.evolve(rho) chan = qf.CNot(0, 1).aschannel() rho = chan.evolve(rho) prob = rho.probabilities() assert np.allclose(prob, [[0.5, 0], [0, 0.5]])
def test_cswap(): ket = qf.zero_state(3) ket = qf.X(1).run(ket) ket = qf.CSWAP(0, 1, 2).run(ket) assert ket.vec.asarray()[0, 1, 0] == ALMOST_ONE ket = qf.X(0).run(ket) ket = qf.CSWAP(0, 1, 2).run(ket) assert ket.vec.asarray()[1, 0, 1] == ALMOST_ONE
def test_sample_bell(): rho = qf.zero_state(2).asdensity() chan = qf.H(0).aschannel() rho = chan.evolve(rho) chan = qf.CNOT(0, 1).aschannel() # TODO: chanmul rho = chan.evolve(rho) prob = qf.asarray(rho.probabilities()) assert np.allclose(prob, [[0.5, 0], [0, 0.5]])
def test_bits(): ket = qf.X(0).run(qf.zero_state(4)) assert ket.vec.asarray()[1, 0, 0, 0] == ALMOST_ONE ket = qf.X(1).run(qf.zero_state(4)) assert ket.vec.asarray()[0, 1, 0, 0] == ALMOST_ONE ket = qf.X(2).run(qf.zero_state(4)) assert ket.vec.asarray()[0, 0, 1, 0] == ALMOST_ONE ket = qf.X(3).run(qf.zero_state(4)) assert ket.vec.asarray()[0, 0, 0, 1] == ALMOST_ONE ket = qf.zero_state(8) ket = qf.X(2).run(ket) ket = qf.X(4).run(ket) ket = qf.X(6).run(ket) res = ket.vec.asarray() assert res[0, 0, 1, 0, 1, 0, 1, 0]
def test_conditional_gate() -> None: controlled_gate = qf.conditional_gate(0, qf.X(1), qf.Y(1)) state = qf.zero_state(2) state = controlled_gate.run(state) assert state.tensor[0, 1] == 1.0 state = qf.X(0).run(state) state = controlled_gate.run(state) assert 1.0j * state.tensor[1, 0] == 1.0
def prepare_w16(): """ Prepare a 16-qubit W state using sqrt(iswaps) and local gates, respecting linear topology """ ket = qf.zero_state(16) circ = w16_circuit() ket = circ.run(ket) return ket
def test_conditional_gate(): controlled_gate = qf.conditional_gate(0, qf.X(1), qf.Y(1)) state = qf.zero_state(2) state = controlled_gate.run(state) assert state.vec.asarray()[0, 1] == ALMOST_ONE state = qf.X(0).run(state) state = controlled_gate.run(state) assert 1.0j * state.vec.asarray()[1, 0] == ALMOST_ONE
def test_projectors() -> None: ket = qf.zero_state(1) assert qf.P0(0).run(ket).norm() == 1.0 ket = qf.H(0).run(ket) measure0 = qf.P0(0).run(ket) assert np.isclose(measure0.norm(), 0.5) measure1 = qf.P1(0).run(ket) assert np.isclose(measure1.norm(), 0.5)