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_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_cu1() -> None: # Test that QASM's cu1 gate is the same as CPHASE up to global phase for _ in range(REPS): theta = random.uniform(0, 4) circ0 = qf.Circuit([ qf.Rz(theta / 2, 0), qf.CNot(0, 1), qf.Rz(-theta / 2, 1), qf.CNot(0, 1), qf.Rz(theta / 2, 1), ]) gate0 = circ0.asgate() gate1 = qf.CPhase(theta, 0, 1) assert qf.gates_close(gate0, gate1)
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_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_phaseshift() -> None: gate = qf.T(0) @ qf.T(0) assert qf.gates_close(gate, gate) assert qf.gates_close(gate, qf.S(0)) assert qf.gates_close(qf.S(0), qf.PhaseShift(np.pi / 2, 0)) assert qf.gates_close(qf.T(0), qf.PhaseShift(np.pi / 4, 0)) # PhaseShift and RZ are the same up to a global phase. for _ in range(REPS): theta = random.uniform(-4 * np.pi, +4 * np.pi) assert qf.gates_close(qf.Rz(theta, 0), qf.PhaseShift(theta, 0)) # Causes a rounding error that can result in NANs if not corrected for. theta = -2.5700302313621375 assert qf.gates_close(qf.Rz(theta, 0), qf.PhaseShift(theta, 0))
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_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_gate_symbolic_params() -> None: theta = Symbol("θ") gate0 = qf.Rz(theta, 1) assert str(gate0) == "Rz(θ) 1" gate1 = gate0**4 assert str(gate1) == "Rz(4*θ) 1" circ = qf.Circuit([gate0, gate1]) print(circ) diag = qf.circuit_to_diagram(circ) assert diag == "1: ───Rz(θ)───Rz(4*θ)───\n" gate2 = gate0.resolve({"θ": 2}) assert gate2.param("theta") == 2.0 with pytest.raises(KeyError): _ = gate2.param("asldfh")
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))
def test_hadamard() -> None: gate = qf.Rz(np.pi / 2, 0) @ qf.I(0) gate = qf.Rx(np.pi / 2, 0) @ gate gate = qf.Rz(np.pi / 2, 0) @ gate assert qf.gates_close(gate, qf.H(0))