def test_circuit_to_pyquil() -> None: circ = qf.Circuit() circ += qf.X(0) prog = xforest.circuit_to_pyquil(circ) assert str(prog) == "X 0\n" circ = qf.Circuit() circ1 = qf.Circuit() circ2 = qf.Circuit() circ1 += qf.Ry(np.pi / 2, 0) circ1 += qf.Rz(np.pi, 0) circ1 += qf.Ry(np.pi / 2, 1) circ1 += qf.Rx(np.pi, 1) circ1 += qf.CNot(0, 1) circ2 += qf.Rx(-np.pi / 2, 1) circ2 += qf.Ry(4.71572463191, 1) circ2 += qf.Rx(np.pi / 2, 1) circ2 += qf.CNot(0, 1) circ2 += qf.Rx(-2 * 2.74973750579, 0) circ2 += qf.Rx(-2 * 2.74973750579, 1) circ += circ1 circ += circ2 prog = xforest.circuit_to_pyquil(circ) new_circ = xforest.pyquil_to_circuit(prog) assert qf.circuits_close(circ, new_circ)
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_elements() -> None: circ = qf.Circuit() circ1 = qf.Circuit() circ2 = qf.Circuit() circ1 += qf.Ry(np.pi / 2, 0) circ1 += qf.Rx(np.pi, 0) circ1 += qf.Ry(np.pi / 2, 1) circ1 += qf.Rx(np.pi, 1) circ1 += qf.CNot(0, 1) circ2 += qf.Rx(-np.pi / 2, 1) circ2 += qf.Ry(4.71572463191, 1) circ2 += qf.Rx(np.pi / 2, 1) circ2 += qf.CNot(0, 1) circ2 += qf.Rx(-2 * 2.74973750579, 0) circ2 += qf.Rx(-2 * 2.74973750579, 1) circ += circ1 circ += circ2 assert len(circ) == 11 assert circ.size() == 11 assert circ[4].name == "CNot" circ_13 = circ[1:3] assert len(circ_13) == 2 assert isinstance(circ_13, qf.Circuit)
def test_fubini_study_angle() -> None: for _ in range(REPS): theta = random.uniform(-np.pi, +np.pi) ang = qf.fubini_study_angle(qf.I(0).tensor, qf.Rx(theta, 0).su().tensor) assert np.isclose(2 * ang / abs(theta), 1.0) ang = qf.fubini_study_angle(qf.I(0).tensor, qf.Ry(theta, 0).tensor) assert np.isclose(2 * ang / abs(theta), 1.0) ang = qf.fubini_study_angle(qf.I(0).tensor, qf.Rz(theta, 0).tensor) assert np.isclose(2 * ang / abs(theta), 1.0) ang = qf.fubini_study_angle(qf.Swap(0, 1).tensor, qf.PSwap(theta, 0, 1).tensor) assert np.isclose(2 * ang / abs(theta), 1.0) ang = qf.fubini_study_angle(qf.I(0).tensor, qf.PhaseShift(theta, 0).tensor) assert np.isclose(2 * ang / abs(theta), 1.0) assert qf.fubini_study_close(qf.Rz(theta, 0).tensor, qf.Rz(theta, 0).tensor) for n in range(1, 6): eye = qf.IdentityGate(list(range(n))) assert np.isclose(qf.fubini_study_angle(eye.tensor, eye.tensor), 0.0) with pytest.raises(ValueError): qf.fubini_study_angle(qf.RandomGate([1]).tensor, qf.RandomGate([0, 1]).tensor)
def test_qaoa_circuit() -> None: circ = qf.Circuit() circ += qf.Ry(np.pi / 2, 0) circ += qf.Rx(np.pi, 0) circ += qf.Ry(np.pi / 2, 1) circ += qf.Rx(np.pi, 1) circ += qf.CNot(0, 1) circ += qf.Rx(-np.pi / 2, 1) circ += qf.Ry(4.71572463191, 1) circ += qf.Rx(np.pi / 2, 1) circ += qf.CNot(0, 1) circ += qf.Rx(-2 * 2.74973750579, 0) circ += qf.Rx(-2 * 2.74973750579, 1) ket = qf.zero_state(2) ket = circ.run(ket) assert qf.states_close(ket, true_ket())
def test_circuit_to_qutip() -> None: q0, q1, q2 = 0, 1, 2 circ0 = qf.Circuit() circ0 += qf.I(q0) circ0 += qf.Ph(0.1, q0) circ0 += qf.X(q0) circ0 += qf.Y(q1) circ0 += qf.Z(q0) circ0 += qf.S(q1) circ0 += qf.T(q2) circ0 += qf.H(q0) circ0 += qf.H(q1) circ0 += qf.H(q2) circ0 += qf.CNot(q0, q1) circ0 += qf.CNot(q1, q0) circ0 += qf.Swap(q0, q1) circ0 += qf.ISwap(q0, q1) circ0 += qf.CCNot(q0, q1, q2) circ0 += qf.CSwap(q0, q1, q2) circ0 == qf.I(q0) circ0 += qf.Rx(0.1, q0) circ0 += qf.Ry(0.2, q1) circ0 += qf.Rz(0.3, q2) circ0 += qf.V(q0) circ0 += qf.H(q1) circ0 += qf.CY(q0, q1) circ0 += qf.CZ(q0, q1) circ0 += qf.CS(q1, q2) circ0 += qf.CT(q0, q1) circ0 += qf.SqrtSwap(q0, q1) circ0 += qf.SqrtISwap(q0, q1) circ0 += qf.CCNot(q0, q1, q2) circ0 += qf.CSwap(q0, q1, q2) circ0 += qf.CPhase(0.1, q1, q2) # Not yet supported # circ0 += qf.B(q1, q2) # circ0 += qf.Swap(q1, q2) ** 0.1 qbc = xqutip.circuit_to_qutip(circ0) U = gate_sequence_product(qbc.propagators()) gate0 = qf.Unitary(U.full(), qubits=[0, 1, 2]) assert qf.gates_close(gate0, circ0.asgate()) circ1 = xqutip.qutip_to_circuit(qbc) assert qf.gates_close(circ0.asgate(), circ1.asgate())
def test_qaoa() -> None: ket_true = [ 0.00167784 + 1.00210180e-05 * 1j, 0.5 - 4.99997185e-01 * 1j, 0.5 - 4.99997185e-01 * 1j, 0.00167784 + 1.00210180e-05 * 1j, ] rho_true = qf.State(ket_true).asdensity() rho = qf.zero_state(2).asdensity() rho = qf.Ry(np.pi / 2, 0).aschannel().evolve(rho) rho = qf.Rx(np.pi, 0).aschannel().evolve(rho) rho = qf.Ry(np.pi / 2, 1).aschannel().evolve(rho) rho = qf.Rx(np.pi, 1).aschannel().evolve(rho) rho = qf.CNot(0, 1).aschannel().evolve(rho) rho = qf.Rx(-np.pi / 2, 1).aschannel().evolve(rho) rho = qf.Ry(4.71572463191, 1).aschannel().evolve(rho) rho = qf.Rx(np.pi / 2, 1).aschannel().evolve(rho) rho = qf.CNot(0, 1).aschannel().evolve(rho) rho = qf.Rx(-2 * 2.74973750579, 0).aschannel().evolve(rho) rho = qf.Rx(-2 * 2.74973750579, 1).aschannel().evolve(rho) assert qf.densities_close(rho, rho_true)
def test_rn() -> None: theta = 1.23 gate = qf.Rn(theta, 1, 0, 0, "q0") assert qf.gates_close(gate, qf.Rx(theta, "q0")) gate = qf.Rn(theta, 0, 1, 0, "q0") assert qf.gates_close(gate, qf.Ry(theta, "q0")) gate = qf.Rn(theta, 0, 0, 1, "q0") assert qf.gates_close(gate, qf.Rz(theta, "q0")) gate = qf.Rn(np.pi, 1 / np.sqrt(2), 0, 1 / np.sqrt(2), "q0") assert qf.gates_close(gate, qf.H("q0"))
def test_qubit_qaoa_circuit() -> None: # Adapted from reference QVM wf_true = np.array([ 0.00167784 + 1.00210180e-05 * 1j, 0.50000000 - 4.99997185e-01 * 1j, 0.50000000 - 4.99997185e-01 * 1j, 0.00167784 + 1.00210180e-05 * 1j, ]) ket_true = qf.State(wf_true.reshape((2, 2))) ket = qf.zero_state(2) ket = qf.Ry(np.pi / 2, 0).run(ket) ket = qf.Rx(np.pi, 0).run(ket) ket = qf.Ry(np.pi / 2, 1).run(ket) ket = qf.Rx(np.pi, 1).run(ket) ket = qf.CNot(0, 1).run(ket) ket = qf.Rx(-np.pi / 2, 1).run(ket) ket = qf.Ry(4.71572463191, 1).run(ket) ket = qf.Rx(np.pi / 2, 1).run(ket) ket = qf.CNot(0, 1).run(ket) ket = qf.Rx(-2 * 2.74973750579, 0).run(ket) ket = qf.Rx(-2 * 2.74973750579, 1).run(ket) assert qf.states_close(ket, ket_true)
def test_visualize_circuit() -> None: circ = qf.Circuit() circ += qf.I(7) circ += qf.X(0) circ += qf.Y(1) circ += qf.Z(2) circ += qf.H(3) circ += qf.S(4) circ += qf.T(5) circ += qf.S_H(6) circ += qf.T_H(7) circ += qf.Rx(-0.5 * pi, 0) circ += qf.Ry(0.5 * pi, 4) circ += qf.Rz((1 / 3) * pi, 5) circ += qf.Ry(0.222, 6) circ += qf.XPow(0.5, 0) circ += qf.YPow(0.5, 2) circ += qf.ZPow(0.4, 2) circ += qf.HPow(0.5, 3) circ += qf.ZPow(0.47276, 1) # Gate with symbolic parameter # gate = qf.Rz(Symbol('\\theta'), 1) # circ += gate circ += qf.CNot(1, 2) circ += qf.CNot(2, 1) # circ += qf.IDEN(*range(8)) circ += qf.ISwap(4, 2) circ += qf.ISwap(6, 5) circ += qf.CZ(1, 3) circ += qf.Swap(1, 5) # circ += qf.Barrier(0, 1, 2, 3, 4, 5, 6) # Not yet supported in latex circ += qf.CCNot(1, 2, 3) circ += qf.CSwap(4, 5, 6) circ += qf.P0(0) circ += qf.P1(1) circ += qf.Reset(2) circ += qf.Reset(4, 5, 6) circ += qf.H(4) circ += qf.XX(0.25, 1, 4) circ += qf.XX(0.25, 1, 2) circ += qf.YY(0.75, 1, 3) circ += qf.ZZ(1 / 3, 3, 1) circ += qf.CPhase(0, 0, 1) circ += qf.CPhase(pi * 1 / 2, 0, 4) circ += qf.Can(1 / 3, 1 / 2, 1 / 2, 0, 1) circ += qf.Can(1 / 3, 1 / 2, 1 / 2, 2, 4) circ += qf.Can(1 / 3, 1 / 2, 1 / 2, 6, 5) # circ += qf.Measure(0) # circ += qf.Measure(1, 1) circ += qf.PSwap(pi / 2, 6, 7) circ += qf.Ph(1 / 4, 7) circ += qf.CH(1, 6) circ += qf.visualization.NoWire([0, 1, 2]) # circ += qf.visualization.NoWire(4, 1, 2) if os.environ.get("QF_VIZTEST"): print() print(qf.circuit_to_diagram(circ)) qf.circuit_to_diagram(circ) qf.circuit_to_latex(circ) qf.circuit_to_latex(circ, package="qcircuit") qf.circuit_to_latex(circ, package="quantikz") qf.circuit_to_diagram(circ) qf.circuit_to_diagram(circ, use_unicode=False) latex = qf.circuit_to_latex(circ, package="qcircuit") print(latex) if os.environ.get("QF_VIZTEST"): qf.latex_to_image(latex).show() latex = qf.circuit_to_latex(circ, package="quantikz") print(latex) if os.environ.get("QF_VIZTEST"): qf.latex_to_image(latex).show()
def test_circuit_to_quirk() -> None: # 2-qubit gates quirk = "https://algassert.com/quirk#circuit={%22cols%22:[[1,%22X%22,%22%E2%80%A2%22],[%22%E2%80%A2%22,1,%22Z%22],[1,%22%E2%80%A2%22,%22Y%22],[%22Swap%22,1,%22Swap%22]]}" # noqa: E501 circ = qf.Circuit([qf.CNot(2, 1), qf.CZ(0, 2), qf.CY(1, 2), qf.Swap(0, 2)]) print() print(urllib.parse.unquote(quirk)) print(quirk_url(circuit_to_quirk(circ))) assert urllib.parse.unquote(quirk) == quirk_url(circuit_to_quirk(circ)) # 3-qubit gates quirk = "https://algassert.com/quirk#circuit={%22cols%22:[[%22%E2%80%A2%22,%22%E2%80%A2%22,%22X%22],[%22%E2%80%A2%22,%22%E2%80%A2%22,%22Z%22],[%22%E2%80%A2%22,%22Swap%22,%22Swap%22]]}" # noqa: E501 circ = qf.Circuit([qf.CCNot(0, 1, 2), qf.CCZ(0, 1, 2), qf.CSwap(0, 1, 2)]) print() print(urllib.parse.unquote(quirk)) print(quirk_url(circuit_to_quirk(circ))) assert urllib.parse.unquote(quirk) == quirk_url(circuit_to_quirk(circ)) test0 = "https://algassert.com/quirk#circuit={%22cols%22:[[%22Z%22,%22Y%22,%22X%22,%22H%22]]}" # noqa: E501 test0 = urllib.parse.unquote(test0) circ = qf.Circuit([qf.Z(0), qf.Y(1), qf.X(2), qf.H(3)]) print(test0) print(quirk_url(circuit_to_quirk(circ))) assert test0 == quirk_url(circuit_to_quirk(circ)) test_halfturns = "https://algassert.com/quirk#circuit={%22cols%22:[[%22X^%C2%BD%22,%22Y^%C2%BD%22,%22Z^%C2%BD%22],[%22X^-%C2%BD%22,%22Y^-%C2%BD%22,%22Z^-%C2%BD%22]]}" # noqa: E501 test_halfturns = urllib.parse.unquote(test_halfturns) circ = qf.Circuit( [qf.V(0), qf.SqrtY(1), qf.S(2), qf.V(0).H, qf.SqrtY(1).H, qf.S(2).H]) print(test_halfturns) print(quirk_url(circuit_to_quirk(circ))) assert test_halfturns == quirk_url(circuit_to_quirk(circ)) quarter_turns = "https://algassert.com/quirk#circuit={%22cols%22:[[%22Z^%C2%BC%22],[%22Z^-%C2%BC%22]]}" # noqa: E501 s = urllib.parse.unquote(quarter_turns) circ = qf.Circuit([qf.T(0), qf.T(0).H]) assert s == quirk_url(circuit_to_quirk(circ)) # GHZ circuit quirk = "https://algassert.com/quirk#circuit={%22cols%22:[[%22H%22],[%22%E2%80%A2%22,%22X%22],[1,%22%E2%80%A2%22,%22X%22]]}" # noqa: E501 circ = qf.Circuit([qf.H(0), qf.CNot(0, 1), qf.CNot(1, 2)]) print(urllib.parse.unquote(quirk)) print(quirk_url(circuit_to_quirk(circ))) assert urllib.parse.unquote(quirk) == quirk_url(circuit_to_quirk(circ)) test_formulaic = "https://algassert.com/quirk#circuit={%22cols%22:[[{%22id%22:%22X^ft%22,%22arg%22:%220.1%22},{%22id%22:%22Y^ft%22,%22arg%22:%220.2%22},{%22id%22:%22Z^ft%22,%22arg%22:%220.3%22}],[{%22id%22:%22Rxft%22,%22arg%22:%220.4%22},{%22id%22:%22Ryft%22,%22arg%22:%220.5%22},{%22id%22:%22Rzft%22,%22arg%22:%220.6%22}]]}" # noqa: E501 s = urllib.parse.unquote(test_formulaic) circ = qf.Circuit([ qf.XPow(0.1, 0), qf.YPow(0.2, 1), qf.ZPow(0.3, 2), qf.Rx(0.4, 0), qf.Ry(0.5, 1), qf.Rz(0.6, 2), ]) assert s == quirk_url(circuit_to_quirk(circ))
def test_rotation_gates() -> None: assert qf.gates_close(qf.I(0), qf.I(0)) assert qf.gates_close(qf.Rx(np.pi, 0), qf.X(0)) assert qf.gates_close(qf.Ry(np.pi, 0), qf.Y(0)) assert qf.gates_close(qf.Rz(np.pi, 0), qf.Z(0))