def test_CH(): # Construct a controlled Hadamard gate gate = qf.identity_gate(2) gate = qf.S(1).H @ gate gate = qf.H(1) @ gate gate = qf.T(1).H @ gate gate = qf.CNOT(0, 1) @ gate gate = qf.T(1) @ gate gate = qf.H(1) @ gate gate = qf.S(1) @ gate # Do nothing ket = qf.zero_state(2) ket = gate.run(ket) assert qf.states_close(ket, qf.zero_state(2)) # Do nothing ket = qf.zero_state(2) ket = qf.X(0).run(ket) ket = gate.run(ket) ket = qf.H(1).run(ket) ket = qf.X(0).run(ket) assert qf.states_close(ket, qf.zero_state(2))
def test_CH() -> None: gate1 = qf.CH(0, 1) # I picked up this circuit for a CH gate from qiskit # qiskit/extensions/standard/ch.py # But it clearly far too long. CH is locally equivalent to CNOT, # so requires only one CNOT gate. circ2 = qf.Circuit([ qf.H(1), qf.S_H(1), qf.CNot(0, 1), qf.H(1), qf.T(1), qf.CNot(0, 1), qf.T(1), qf.H(1), qf.S(1), qf.X(1), qf.S(0), ]) assert qf.gates_close(gate1, circ2.asgate()) # Here's a better decomposition circ1 = qf.Circuit([qf.YPow(+0.25, 1), qf.CNot(0, 1), qf.YPow(-0.25, 1)]) assert qf.gates_close(gate1, circ1.asgate()) assert qf.circuits_close(circ1, circ2)
def test_gates_commute() -> None: assert qf.gates_commute(qf.X(0), qf.X(0)) assert not qf.gates_commute(qf.X(0), qf.T(0)) assert qf.gates_commute(qf.X(0), qf.T(1)) assert qf.gates_commute(qf.S(0), qf.T(0)) assert qf.gates_commute(qf.S(0), qf.T(0)) assert qf.gates_commute(qf.XX(0.1, 0, 1), qf.X(0)) assert not qf.gates_commute(qf.ZZ(0.1, 0, 1), qf.X(0))
def test_unitary_1qubit(): assert qf.almost_unitary(qf.X()) assert qf.almost_unitary(qf.Y()) assert qf.almost_unitary(qf.Z()) assert qf.almost_unitary(qf.H()) assert qf.almost_unitary(qf.S()) assert qf.almost_unitary(qf.T())
def test_phase(): gate = qf.T(0) @ qf.T(0) assert qf.gates_close(gate, gate) assert qf.gates_close(gate, qf.S()) assert qf.gates_close(qf.S(), qf.PHASE(pi / 2)) assert qf.gates_close(qf.T(), qf.PHASE(pi / 4)) # PHASE and RZ are the same up to a global phase. for _ in range(REPS): theta = random.uniform(-4 * pi, +4 * pi) assert qf.gates_close(qf.RZ(theta), qf.PHASE(theta)) # Causes a rounding error that can result in NANs if not corrected for. theta = -2.5700302313621375 assert qf.gates_close(qf.RZ(theta), qf.PHASE(theta))
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_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_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 _cli(): gates = [ qf.I(0), qf.X(0), qf.Y(0), qf.Z(0), qf.S(0), qf.T(0), qf.H(0), qf.XPow(0.2, 0), qf.YPow(0.2, 0), qf.ZPow(0.2, 0), qf.CNot(0, 1), qf.CZ(0, 1), qf.Swap(0, 1), qf.ISwap(0, 1), qf.CCNot(0, 1, 2), qf.CCZ(0, 1, 2), qf.CSwap(0, 1, 2), ] print() print("Gate QF GOPS Cirq GOPS") # for n in range(4): # circ = benchmark_circuit(QUBITS, GATES, qf.RandomGate([0,1])) # t = timeit.timeit(lambda: circ.run(), number=REPS, # timer=time.process_time) # gops = int((GATES*REPS)/t) # gops = int((gops * 100) + 0.5) / 100.0 # print(f"gate qubits: {n} gops:{gops}") for gate in gates: circ = benchmark_circuit(QUBITS, GATES, gate) t = timeit.timeit(lambda: circ.run(), number=REPS, timer=time.process_time) cq = qf.xcirq.CirqSimulator(circ) t2 = timeit.timeit(lambda: cq.run(), number=REPS, timer=time.process_time) gops = int((GATES * REPS) / t) gops = int((gops * 100) + 0.5) / 100.0 gops2 = int((GATES * REPS) / t2) gops2 = int((gops2 * 100) + 0.5) / 100.0 if gops / gops2 > 0.8: print(gate.name, "\t", gops, "\t", gops2) else: print(gate.name, "\t", gops, "\t", gops2, "\t☹️")
def benchmark(N, gates): """Create and run a circuit with N qubits and given number of gates""" qubits = list(range(0, N)) ket = qf.zero_state(N) for n in range(0, N): ket = qf.H(n).run(ket) for _ in range(0, (gates - N) // 3): qubit0, qubit1 = random.sample(qubits, 2) ket = qf.X(qubit0).run(ket) ket = qf.T(qubit1).run(ket) ket = qf.CNOT(qubit0, qubit1).run(ket) return ket.vec.tensor
def test_circuit_to_circ() -> None: q0, q1, q2 = "q0", "q1", "q2" circ0 = qf.Circuit() circ0 += qf.I(q0) circ0 += qf.X(q1) circ0 += qf.Y(q2) 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.XPow(0.6, q0) circ0 += qf.YPow(0.6, q1) circ0 += qf.ZPow(0.6, q2) circ0 += qf.XX(0.2, q0, q1) circ0 += qf.YY(0.3, q1, q2) circ0 += qf.ZZ(0.4, q2, q0) circ0 += qf.CZ(q0, q1) circ0 += qf.CNot(q0, q1) circ0 += qf.Swap(q0, q1) circ0 += qf.ISwap(q0, q1) circ0 += qf.CCZ(q0, q1, q2) circ0 += qf.CCNot(q0, q1, q2) circ0 += qf.CSwap(q0, q1, q2) circ0 += qf.FSim(1, 2, q0, q1) diag0 = qf.circuit_to_diagram(circ0) # print() # print(diag0) cqc = circuit_to_cirq(circ0) # print(cqc) circ1 = cirq_to_circuit(cqc) diag1 = qf.circuit_to_diagram(circ1) # print() # print(diag1) assert diag0 == diag1
def test_cirq_simulator() -> None: q0, q1, q2 = "q0", "q1", "q2" circ0 = qf.Circuit() circ0 += qf.I(q0) circ0 += qf.I(q1) circ0 += qf.I(q2) circ0 += qf.X(q1) circ0 += qf.Y(q2) 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.XPow(0.6, q0) circ0 += qf.YPow(0.6, q1) circ0 += qf.ZPow(0.6, q2) circ0 += qf.XX(0.2, q0, q1) circ0 += qf.YY(0.3, q1, q2) circ0 += qf.ZZ(0.4, q2, q0) circ0 += qf.CZ(q0, q1) circ0 += qf.CNot(q0, q1) circ0 += qf.Swap(q0, q1) circ0 += qf.ISwap(q0, q1) circ0 += qf.CCZ(q0, q1, q2) circ0 += qf.CCNot(q0, q1, q2) circ0 += qf.CSwap(q0, q1, q2) ket0 = qf.random_state([q0, q1, q2]) ket1 = circ0.run(ket0) sim = CirqSimulator(circ0) ket2 = sim.run(ket0) assert ket1.qubits == ket2.qubits print(qf.state_angle(ket1, ket2)) assert qf.states_close(ket1, ket2) assert qf.states_close(circ0.run(), sim.run())
def test_qsim_simulator() -> None: q0, q1, q2 = "q0", "q1", "q2" circ0 = qf.Circuit() circ0 += qf.I(q0) circ0 += qf.H(q0) circ0 += qf.Z(q1) circ0 += qf.Z(q2) circ0 += qf.Z(q1) circ0 += qf.S(q1) circ0 += qf.T(q2) circ0 += qf.H(q0) circ0 += qf.H(q2) # Waiting for bugfix in qsim circ0 += qf.Z(q1)**0.2 circ0 += qf.X(q1)**0.2 circ0 += qf.XPow(0.2, q0) circ0 += qf.YPow(0.2, q1) circ0 += qf.ZPow(0.5, q2) circ0 += qf.CZ(q0, q1) circ0 += qf.CNot(q0, q1) # circ0 += qf.SWAP(q0, q1) # No SWAP! # circ0 += qf.ISWAP(q0, q1) # Waiting for bugfix in qsim circ0 += qf.FSim(0.1, 0.2, q0, q1) # No 3-qubit gates # Initial state not yet supported in qsim # ket0 = qf.random_state([q0, q1, q2]) ket1 = circ0.run() sim = QSimSimulator(circ0) ket2 = sim.run() assert ket1.qubits == ket2.qubits print("QF", ket1) print("QS", ket2) assert qf.states_close(ket1, ket2) assert qf.states_close(circ0.run(), sim.run())
def test_parametric_Z(): assert qf.gates_close(qf.TZ(1 / 4), qf.T()) assert qf.gates_close(qf.TZ(1 / 2), qf.S()) assert qf.gates_close(qf.TZ(1.0), qf.Z())
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_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_parametric_Z() -> None: assert qf.gates_close(qf.ZPow(0.25, 1), qf.T(1)) assert qf.gates_close(qf.ZPow(0.5, 2), qf.S(2)) assert qf.gates_close(qf.ZPow(1.0, 3), qf.Z(3))