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_circuit_wires(): circ = qf.Circuit() circ += qf.TY(1 / 2, 0) circ += qf.TX(1, 10) circ += qf.TY(1 / 2, 1) circ += qf.TX(1, 1) circ += qf.CNOT(0, 4) bits = circ.qubits assert bits == (0, 1, 4, 10)
def test_diamond_norm(): # Test cases borrowed from qutip, # https://github.com/qutip/qutip/blob/master/qutip/tests/test_metrics.py # which were in turn generated using QuantumUtils for MATLAB # (https://goo.gl/oWXhO9) RTOL = 0.01 chan0 = qf.I(0).aschannel() chan1 = qf.X(0).aschannel() dn = qf.diamond_norm(chan0, chan1) assert np.isclose(2.0, dn, rtol=RTOL) turns_dnorm = [[1.000000e-03, 3.141591e-03], [3.100000e-03, 9.738899e-03], [1.000000e-02, 3.141463e-02], [3.100000e-02, 9.735089e-02], [1.000000e-01, 3.128689e-01], [3.100000e-01, 9.358596e-01]] for turns, target in turns_dnorm: chan0 = qf.TX(0).aschannel() chan1 = qf.TX(turns).aschannel() dn = qf.diamond_norm(chan0, chan1) assert np.isclose(target, dn, rtol=RTOL) hadamard_mixtures = [[1.000000e-03, 2.000000e-03], [3.100000e-03, 6.200000e-03], [1.000000e-02, 2.000000e-02], [3.100000e-02, 6.200000e-02], [1.000000e-01, 2.000000e-01], [3.100000e-01, 6.200000e-01]] for p, target in hadamard_mixtures: # FIXME: implement __rmul__ for channels chan0 = qf.I(0).aschannel() * (1 - p) + qf.H(0).aschannel() * p chan1 = qf.I(0).aschannel() dn = qf.diamond_norm(chan0, chan1) assert np.isclose(dn, target, rtol=RTOL) chan0 = qf.TY(0.5, 0).aschannel() chan1 = qf.I(0).aschannel() dn = qf.diamond_norm(chan0, chan1) assert np.isclose(dn, np.sqrt(2), rtol=RTOL) chan0 = qf.CNOT(0, 1).aschannel() chan1 = qf.CNOT(1, 0).aschannel() qf.diamond_norm(chan0, chan1)
def test_parametric_gates1(): for _ in range(REPS): theta = random.uniform(-4 * pi, +4 * pi) assert qf.almost_unitary(qf.RX(theta)) assert qf.almost_unitary(qf.RY(theta)) assert qf.almost_unitary(qf.RZ(theta)) for _ in range(REPS): theta = random.uniform(-4 * pi, +4 * pi) assert qf.almost_unitary(qf.TX(theta)) assert qf.almost_unitary(qf.TY(theta)) assert qf.almost_unitary(qf.TZ(theta)) for _ in range(REPS): theta = random.uniform(-4 * pi, +4 * pi) assert qf.almost_unitary(qf.CPHASE00(theta)) assert qf.almost_unitary(qf.CPHASE01(theta)) assert qf.almost_unitary(qf.CPHASE10(theta)) assert qf.almost_unitary(qf.CPHASE(theta)) assert qf.almost_unitary(qf.PSWAP(theta)) assert qf.gates_close(qf.I(), qf.I()) assert qf.gates_close(qf.RX(pi), qf.X()) assert qf.gates_close(qf.RY(pi), qf.Y()) assert qf.gates_close(qf.RZ(pi), qf.Z())
def test_parametric_TX_TY_TZ(): gate = qf.I() gate = qf.TZ(1 / 2) @ gate gate = qf.TX(1 / 2) @ gate gate = qf.TZ(1 / 2) @ gate assert qf.gates_close(gate, qf.H())
def test_gates_to_latex(): 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, 1) circ += qf.RZ((1/3)*pi, 1) circ += qf.RY(0.222, 1) circ += qf.TX(0.5, 0) circ += qf.TY(0.5, 1) circ += qf.TZ(0.4, 1) circ += qf.TZ(0.47276, 1) # Gate with cunning hack gate = qf.RZ(0.4, 1) gate.params['theta'] = qf.Parameter('\\theta') circ += gate circ += qf.CNOT(1, 2) circ += qf.CNOT(2, 1) circ += qf.CZ(1, 3) circ += qf.SWAP(1, 5) circ += qf.ISWAP(4, 2) # circ += qf.Barrier(0, 1, 2, 3, 4, 5, 6) # Not yet supported 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.Reset() # FIXME. Should fail with clear error message circ += qf.XX(0.25, 1, 3) circ += qf.YY(0.75, 1, 3) circ += qf.ZZ(1/3, 3, 1) circ += qf.Measure(0) latex = qf.circuit_to_latex(circ) print(latex)
def _test_circ(): # Adapted from referenceQVM circ = qf.Circuit() circ += qf.TY(1 / 2, 0) circ += qf.TX(1, 0) circ += qf.TY(1 / 2, 1) circ += qf.TX(1, 1) circ += qf.CNOT(0, 1) circ += qf.TX(-1 / 2, 1) circ += qf.TY(4.71572463191 / np.pi, 1) circ += qf.TX(1 / 2, 1) circ += qf.CNOT(0, 1) circ += qf.TX(-2 * 2.74973750579 / np.pi, 0) circ += qf.TX(-2 * 2.74973750579 / np.pi, 1) return circ
def test_qaoa_circuit_turns(): circ = qf.Circuit() circ += qf.TY(1 / 2, 0) circ += qf.TX(1, 0) circ += qf.TY(1 / 2, 1) circ += qf.TX(1, 1) circ += qf.CNOT(0, 1) circ += qf.TX(-1 / 2, 1) circ += qf.TY(4.71572463191 / pi, 1) circ += qf.TX(1 / 2, 1) circ += qf.CNOT(0, 1) circ += qf.TX(-2 * 2.74973750579 / pi, 0) circ += qf.TX(-2 * 2.74973750579 / pi, 1) ket = qf.zero_state(2) ket = circ.run(ket) assert qf.states_close(ket, true_ket())
def test_gatepow(): gates = [ qf.I(), qf.X(), qf.Y(), qf.Z(), qf.H(), qf.S(), qf.T(), qf.PHASE(0.1), qf.RX(0.2), qf.RY(0.3), qf.RZ(0.4), qf.CZ(), qf.CNOT(), qf.SWAP(), qf.ISWAP(), qf.CPHASE00(0.5), qf.CPHASE01(0.6), qf.CPHASE10(0.6), qf.CPHASE(0.7), qf.PSWAP(0.15), qf.CCNOT(), qf.CSWAP(), qf.TX(2.7), qf.TY(1.2), qf.TZ(0.3), qf.ZYZ(3.5, 0.9, 2.1), qf.CANONICAL(0.1, 0.2, 7.4), qf.XX(1.8), qf.YY(0.9), qf.ZZ(0.45), qf.PISWAP(0.2), qf.EXCH(0.1), qf.TH(0.3) ] for gate in gates: assert qf.gates_close(gate.H, gate**-1) for gate in gates: sqrt_gate = gate**(1 / 2) two_gate = sqrt_gate @ sqrt_gate assert qf.gates_close(gate, two_gate) for gate in gates: gate0 = gate**0.3 gate1 = gate**0.7 gate2 = gate0 @ gate1 assert qf.gates_close(gate, gate2) for K in range(1, 5): gate = qf.random_gate(K) # FIXME: Throw error on K=0 sqrt_gate = gate**0.5 two_gate = sqrt_gate @ sqrt_gate assert qf.gates_close(gate, two_gate) for gate in gates: rgate = qf.Gate((gate**0.5).tensor) tgate = rgate @ rgate assert qf.gates_close(gate, tgate)