def test_gatemul(): # three cnots same as one swap gate0 = qf.identity_gate([0, 1]) gate1 = qf.CNOT(1, 0) gate2 = qf.CNOT(0, 1) gate3 = qf.CNOT(1, 0) gate = gate0 gate = gate1 @ gate gate = gate2 @ gate gate = gate3 @ gate assert qf.gates_close(gate, qf.SWAP()) # Again, but with labels gate0 = qf.identity_gate(['a', 'b']) gate1 = qf.CNOT('b', 'a') gate2 = qf.CNOT('a', 'b') gate3 = qf.CNOT('b', 'a') gate = gate0 gate = gate1 @ gate gate = gate2 @ gate gate = gate3 @ gate assert qf.gates_close(gate, qf.SWAP('a', 'b')) gate4 = qf.X('a') gate = gate4 @ gate with pytest.raises(NotImplementedError): gate = gate4 @ 3
def test_decomp_stdgates(): gate0 = qf.I(0, 1) gate1 = qf.canonical_decomposition(gate0).asgate() assert qf.gates_close(gate0, gate1) gate0 = qf.CNOT(0, 1) gate1 = qf.canonical_decomposition(gate0).asgate() assert qf.gates_close(gate0, gate1) gate0 = qf.SWAP(0, 1) gate1 = qf.canonical_decomposition(gate0).asgate() assert qf.gates_close(gate0, gate1) gate0 = qf.ISWAP(0, 1) gate1 = qf.canonical_decomposition(gate0).asgate() assert qf.gates_close(gate0, gate1) gate0 = qf.CNOT(0, 1)**0.5 gate1 = qf.canonical_decomposition(gate0).asgate() assert qf.gates_close(gate0, gate1) gate0 = qf.SWAP(0, 1)**0.5 gate1 = qf.canonical_decomposition(gate0).asgate() assert qf.gates_close(gate0, gate1) gate0 = qf.ISWAP(0, 1)**0.5 gate1 = qf.canonical_decomposition(gate0).asgate() assert qf.gates_close(gate0, gate1)
def test_fubini_study_angle(): for _ in range(REPS): theta = random.uniform(-pi, +pi) ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RX(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RY(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RZ(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE ang = qf.asarray( qf.fubini_study_angle(qf.SWAP().vec, qf.PSWAP(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.PHASE(theta).vec)) assert 2 * ang / abs(theta) == ALMOST_ONE for n in range(1, 6): eye = qf.identity_gate(n) assert qf.asarray(qf.fubini_study_angle(eye.vec, eye.vec)) \ == ALMOST_ZERO with pytest.raises(ValueError): qf.fubini_study_angle(qf.random_gate(1).vec, qf.random_gate(2).vec)
def test_cnot(): # three cnots same as one swap gate = qf.identity_gate(2) gate = qf.CNOT(1, 0) @ gate gate = qf.CNOT(0, 1) @ gate gate = qf.CNOT(1, 0) @ gate res = qf.asarray(qf.inner_product(gate.vec, qf.SWAP().vec)) assert abs(res) / 4 == ALMOST_ONE
def test_pswap(): for _ in range(REPS): theta = random.uniform(-4 * pi, +4 * pi) assert qf.almost_unitary(qf.PSWAP(theta)) assert qf.gates_close(qf.SWAP(), qf.PSWAP(0)) assert qf.gates_close(qf.ISWAP(), qf.PSWAP(pi / 2))
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_transpose_map(): # The transpose map is a superoperator that transposes a 1-qubit # density matrix. Not physical. # quant-ph/0202124 ops = [ qf.Gate(np.asarray([[1, 0], [0, 0]])), qf.Gate(np.asarray([[0, 0], [0, 1]])), qf.Gate(np.asarray([[0, 1], [1, 0]]) / np.sqrt(2)), qf.Gate(np.asarray([[0, 1], [-1, 0]]) / np.sqrt(2)) ] kraus = qf.Kraus(ops, weights=(1, 1, 1, -1)) rho0 = qf.random_density(1) rho1 = kraus.evolve(rho0) op0 = qf.asarray(rho0.asoperator()) op1 = qf.asarray(rho1.asoperator()) assert np.allclose(op0.T, op1) # The Choi matrix should be same as SWAP operator choi = kraus.aschannel().choi() choi = qf.asarray(choi) assert np.allclose(choi, qf.asarray(qf.SWAP(0, 2).asoperator()))
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)
def test_unitary_2qubit(): assert qf.almost_unitary(qf.CZ()) assert qf.almost_unitary(qf.CNOT()) assert qf.almost_unitary(qf.SWAP()) assert qf.almost_unitary(qf.ISWAP())
def test_su(): su = qf.SWAP(0, 1).su() assert np.linalg.det(qf.asarray(su.asoperator())) == ALMOST_ONE
def w16_circuit() -> qf.Circuit: """ Return a circuit that prepares the the 16-qubit W state using\ sqrt(iswaps) and local gates, respecting linear topology """ gates = [ qf.X(7), qf.ISWAP(7, 8)**0.5, qf.S(8), qf.Z(8), qf.SWAP(7, 6), qf.SWAP(6, 5), qf.SWAP(5, 4), qf.SWAP(8, 9), qf.SWAP(9, 10), qf.SWAP(10, 11), qf.ISWAP(4, 3)**0.5, qf.S(3), qf.Z(3), qf.ISWAP(11, 12)**0.5, qf.S(12), qf.Z(12), qf.SWAP(3, 2), qf.SWAP(4, 5), qf.SWAP(11, 10), qf.SWAP(12, 13), qf.ISWAP(2, 1)**0.5, qf.S(1), qf.Z(1), qf.ISWAP(5, 6)**0.5, qf.S(6), qf.Z(6), qf.ISWAP(10, 9)**0.5, qf.S(9), qf.Z(9), qf.ISWAP(13, 14)**0.5, qf.S(14), qf.Z(14), qf.ISWAP(1, 0)**0.5, qf.S(0), qf.Z(0), qf.ISWAP(2, 3)**0.5, qf.S(3), qf.Z(3), qf.ISWAP(5, 4)**0.5, qf.S(4), qf.Z(4), qf.ISWAP(6, 7)**0.5, qf.S(7), qf.Z(7), qf.ISWAP(9, 8)**0.5, qf.S(8), qf.Z(8), qf.ISWAP(10, 11)**0.5, qf.S(11), qf.Z(11), qf.ISWAP(13, 12)**0.5, qf.S(12), qf.Z(12), qf.ISWAP(14, 15)**0.5, qf.S(15), qf.Z(15), ] circ = qf.Circuit(gates) return circ