def test_phase_estimation_circuit(): N = 8 phase = 1 / 4 gate = qf.RZ(-4 * np.pi * phase, N) circ = qf.phase_estimation_circuit(gate, range(N)) res = circ.run().measure()[0:N] est_phase = bitlist_to_int(res) / 2**N assert phase - est_phase == ALMOST_ZERO phase = 12 / 256 gate = qf.RZ(-4 * np.pi * phase, N) circ = qf.phase_estimation_circuit(gate, range(N)) res = circ.run().measure()[0:N] est_phase = bitlist_to_int(res) / 2**N assert phase - est_phase == ALMOST_ZERO gate = qf.ZZ(-4 * phase, N, N + 1) circ = qf.phase_estimation_circuit(gate, range(N)) res = circ.run().measure()[0:N] est_phase = bitlist_to_int(res) / 2**N assert phase - est_phase == ALMOST_ZERO with pytest.raises(ValueError): # Gate and output qubits overlap circ = qf.phase_estimation_circuit(gate, range(N + 1))
def test_addition_circuit(): # Two bit addition circuit circ = qf.addition_circuit([0, 1], [2, 3], [4, 5]) for c0 in range(0, 2): for a0 in range(0, 4): for a1 in range(0, 4): expected = a0 + a1 + c0 b0 = int_to_bitlist(a0, 2) b1 = int_to_bitlist(a1, 2) bc = [c0] bits = tuple(b0 + b1 + bc + [0]) state = np.zeros(shape=[2] * 6) state[bits] = 1 ket = qf.State(state) ket = circ.run(ket) bits = ket.measure() res = bits[[5, 2, 3]] res = bitlist_to_int(res) print(c0, a0, a1, expected, res) assert res == expected # Three bit addition circuit circ = qf.addition_circuit([0, 1, 2], [3, 4, 5], [6, 7]) for c0 in range(0, 2): for a0 in range(0, 8): for a1 in range(0, 8): expected = a0 + a1 + c0 b0 = int_to_bitlist(a0, 3) b1 = int_to_bitlist(a1, 3) bc = [c0] bits = tuple(b0 + b1 + bc + [0]) state = np.zeros(shape=[2] * 8) state[bits] = 1 ket = qf.State(state) ket = circ.run(ket) bits = ket.measure() res = bits[[7, 3, 4, 5]] res = bitlist_to_int(res) print(c0, a0, a1, expected, res) assert res == expected with pytest.raises(ValueError): qf.addition_circuit([0, 1, 2], [3, 4, 5, 6], [7, 8]) with pytest.raises(ValueError): qf.addition_circuit([0, 1, 2], [3, 4, 5], [6, 7, 8])
def test_phase_estimation_circuit_2() -> None: N = 8 phase = 12 / 256 gate = qf.Rz(-4 * np.pi * phase, N) circ = qf.phase_estimation_circuit(gate, range(N)) res = circ.run().measure()[0:N] est_phase = bitlist_to_int(res) / 2 ** N assert np.isclose(phase, est_phase)
def test_phase_estimation_circuit_3() -> None: N = 8 phase = 12 / 256 gate = qf.ZZ(-4 * phase, N, N + 1) circ = qf.phase_estimation_circuit(gate, range(N)) res = circ.run().measure()[0:N] est_phase = bitlist_to_int(res) / 2 ** N assert np.isclose(phase, est_phase) with pytest.raises(ValueError): # Gate and output qubits overlap _ = qf.phase_estimation_circuit(gate, range(N + 1))
def test_bitlist_to_int() -> None: assert utils.bitlist_to_int([1, 0, 0]) == 4
def test_bitlist_to_int(): assert bitlist_to_int([1, 0, 0]) == 4