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_gradient_errors() -> None: circ = qf.Circuit() circ += qf.CPhase(0.2, 0, 1) # Not (currently) differentiable qubits = circ.qubits ket0 = qf.zero_state(qubits) ket1 = qf.random_state(qubits) with pytest.raises(ValueError): qf.state_fidelity_gradients(ket0, ket1, circ) with pytest.raises(ValueError): qf.parameter_shift_circuits(circ, 0) with pytest.raises(ValueError): qf.expectation_gradients(ket0, circ, qf.IdentityGate([0, 1]))
def test_CPhase_gates() -> None: for _ in range(REPS): theta = random.uniform(-4 * np.pi, +4 * np.pi) gate11 = qf.ControlGate([0], qf.PhaseShift(theta, 1)) assert qf.gates_close(gate11, qf.CPhase(theta, 0, 1)) gate00 = qf.X(0) @ qf.IdentityGate([0, 1]) gate00 = qf.X(1) @ gate00 gate00 = gate11 @ gate00 gate00 = qf.X(0) @ gate00 gate00 = qf.X(1) @ gate00 assert qf.gates_close(gate00, qf.CPhase00(theta)) gate10 = qf.X(0) @ qf.IdentityGate([0, 1]) gate10 = qf.X(1) @ gate10 gate10 = qf.CPhase01(theta, 0, 1) @ gate10 gate10 = qf.X(0) @ gate10 gate10 = qf.X(1) @ gate10 assert qf.gates_close(gate10, qf.CPhase10(theta)) gate0 = qf.CPhase(theta) ** 2 gate1 = qf.CPhase(theta * 2) assert qf.gates_close(gate0, gate1)
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_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_CPhase_pow() -> None: gate0 = qf.CZ(0, 1) ** 0.4 gate1 = qf.CPhase(0.4 * np.pi, 0, 1) assert qf.gates_close(gate0, gate1)