def test_components() -> None: circ = qf.Circuit() circ += qf.H(0) circ += qf.H(1) dag = qf.DAGCircuit(circ) assert dag.component_nb() == 2 circ += qf.CNot(0, 1) dag = qf.DAGCircuit(circ) assert dag.component_nb() == 1 circ0 = qf.ghz_circuit([0, 2, 4, 6, 8]) circ1 = qf.ghz_circuit([1, 3, 5, 7, 9]) circ = circ0 + circ1 dag = qf.DAGCircuit(circ) comps = dag.components() assert dag.component_nb() == 2 assert len(comps) == 2 circ0 = qf.Circuit(qf.QFTGate([0, 2, 4, 6]).decompose()) circ1 = qf.ghz_circuit([1, 3, 5, 7]) circ += circ0 circ += circ1 circ += qf.H(10) dag = qf.DAGCircuit(circ) comps = dag.components() assert dag.component_nb() == 3 assert len(comps) == 3
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_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_components(): circ = qf.Circuit() circ += qf.H(0) circ += qf.H(1) dag = qf.DAGCircuit(circ) assert dag.component_nb() == 2 circ += qf.CNOT(0, 1) dag = qf.DAGCircuit(circ) assert dag.component_nb() == 1 circ0 = qf.ghz_circuit([0, 2, 4, 6, 8]) circ1 = qf.ghz_circuit([1, 3, 5, 7, 9]) circ = qf.Circuit() circ.extend(circ0) circ.extend(circ1) dag = qf.DAGCircuit(circ) comps = dag.components() assert dag.component_nb() == 2 circ0 = qf.qft_circuit([0, 2, 4, 6]) circ1 = qf.ghz_circuit([1, 3, 5, 7]) circ.extend(circ0) circ.extend(circ1) circ += qf.H(10) dag = qf.DAGCircuit(circ) comps = dag.components() assert dag.component_nb() == 3 assert len(comps) == 3
def test_join_gates(): gate = qf.join_gates(qf.H(0), qf.X(1)) ket = qf.zero_state(2) ket = gate.run(ket) ket = qf.H(0).run(ket) ket = qf.X(1).run(ket) assert qf.states_close(ket, qf.zero_state(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_cnot_reverse() -> None: # Hadamards reverse control on CNot gate0 = qf.H(0) @ qf.IdentityGate([0, 1]) gate0 = qf.H(1) @ gate0 gate0 = qf.CNot(1, 0) @ gate0 gate0 = qf.H(0) @ gate0 gate0 = qf.H(1) @ gate0 assert qf.gates_close(qf.CNot(0, 1), gate0)
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_circuits_close() -> None: circ0 = qf.Circuit([qf.H(0)]) circ1 = qf.Circuit([qf.H(2)]) assert not qf.circuits_close(circ0, circ1) circ2 = qf.Circuit([qf.X(0)]) assert not qf.circuits_close(circ0, circ2) circ3 = qf.Circuit([qf.H(0)]) assert qf.circuits_close(circ0, circ3)
def test_cnot_reverse(): # Hadamards reverse control on CNOT gate0 = qf.identity_gate(2) gate0 = qf.H(0) @ gate0 gate0 = qf.H(1) @ gate0 gate0 = qf.CNOT(1, 0) @ gate0 gate0 = qf.H(0) @ gate0 gate0 = qf.H(1) @ gate0 assert qf.gates_close(qf.CNOT(), gate0)
def test_count(): circ = qf.Circuit() circ += qf.H(0) circ += qf.H(1) circ += qf.H(1) op_count = qf.count_operations(circ) assert op_count == {qf.H: 3} circ = qf.addition_circuit([0, 1, 2], [3, 4, 5], [6, 7]) op_count = qf.count_operations(circ) assert op_count == {qf.CNOT: 13, qf.CCNOT: 6}
def test_create(): gen = [qf.H(i) for i in range(8)] circ1 = qf.Circuit(list(gen)) circ1.run(qf.zero_state(8)) circ2 = qf.Circuit(gen) circ2.run(qf.zero_state(8)) circ3 = qf.Circuit(qf.H(i) for i in range(8)) circ3.run(qf.zero_state(8))
def test_add() -> None: circ = qf.Circuit() circ += qf.H(0) circ += qf.H(1) circ += qf.H(1) assert len(list(circ)) == 3 circ = circ + circ assert len(circ) == 6 circ += circ assert len(circ) == 12
def test_add(): circ = qf.Circuit() circ += qf.H(0) circ += qf.H(1) circ += qf.H(1) assert len(list(circ)) == 3 circ = circ + circ assert len(list(circ.elements)) == 6 circ += circ assert len(list(circ.elements)) == 12
def test_on() -> None: circ = qf.Circuit() circ += qf.H(0) circ += qf.H(1) dag = qf.DAGCircuit(circ) dag = dag.on(2, 3) assert dag.qubits == (2, 3) dag = dag.rewire({2: 4, 3: 6}) assert dag.qubits == (4, 6) with pytest.raises(ValueError): dag.on(2, 3, 5)
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_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_inverse() -> None: # Random circuit circ = qf.Circuit() circ += qf.YPow(1 / 2, 0) circ += qf.H(0) circ += qf.YPow(1 / 2, 1) circ += qf.XPow(1.23123, 1) circ += qf.CNot(0, 1) circ += qf.XPow(-1 / 2, 1) circ += qf.YPow(4.71572463191 / np.pi, 1) circ += qf.CNot(0, 1) circ += qf.XPow(-2 * 2.74973750579 / np.pi, 0) circ += qf.XPow(-2 * 2.74973750579 / np.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 += circ_inv ket = circ.run(ket) assert qf.states_close(ket, qf.zero_state(2))
def test_control_gate() -> None: gate0 = qf.ControlGate([0], qf.X(1)) gate1 = qf.CNot(0, 1) assert qf.gates_close(gate0, gate1) gateb = qf.ControlGate([1], qf.X(0)) gate2 = qf.CNot(1, 0) assert qf.gates_close(gateb, gate2) gate3 = qf.ControlGate([0], qf.Y(1)) gate4 = qf.CY(0, 1) assert qf.gates_close(gate3, gate4) gate5 = qf.ControlGate([0], qf.Z(1)) gate6 = qf.CZ(0, 1) assert qf.gates_close(gate5, gate6) gate7 = qf.ControlGate([0], qf.H(1)) gate8 = qf.CH(0, 1) assert qf.gates_close(gate7, gate8) gate9 = qf.ControlGate([0, 1], qf.X(2)) gate10 = qf.CCNot(0, 1, 2) assert qf.gates_close(gate9, gate10) gate11 = qf.ControlGate([0], qf.Swap(1, 2)) gate12 = qf.CSwap(0, 1, 2) assert qf.gates_close(gate11, gate12)
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_gradients() -> None: # This test only checks that code runs, not that we get correct answers # graph = nx.grid_graph([2, 3]) graph = nx.grid_graph([2, 1]) layers = 2 params = qf.graph_circuit_params(graph, layers) circ = qf.graph_circuit(graph, layers, params) circ += qf.H((1, 1)) # add a non-parameterized gate. Should be ignored qubits = circ.qubits ket0 = qf.zero_state(qubits) ket1 = qf.random_state(qubits) grads0 = qf.state_fidelity_gradients(ket0, ket1, circ) # print(grads0) _ = qf.state_angle_gradients(ket0, ket1, circ) # print(grads1) proj = qf.Projection([ket1]) grads2 = qf.expectation_gradients(ket0, circ, hermitian=proj) # print(grads2) # Check that qf.expectation_gradients() gives same answers for # fidelity as f.state_fidelity_gradients() for g0, g1 in zip(grads0, grads2): assert np.isclose(g0, g1) print(g0, g1)
def test_RN(): for _ in range(REPS): theta = random.uniform(-2 * pi, +2 * pi) nx = random.uniform(0, 1) ny = random.uniform(0, 1) nz = random.uniform(0, 1) L = np.sqrt(nx**2 + ny**2 + nz**2) nx /= L ny /= L nz /= L gate = qf.RN(theta, nx, ny, nz) assert qf.almost_unitary(gate) gate2 = qf.RN(-theta, nx, ny, nz) assert qf.gates_close(gate.H, gate2) assert qf.gates_close(gate**-1, gate2) theta = 1.23 gate = qf.RN(theta, 1, 0, 0) assert qf.gates_close(gate, qf.RX(theta)) gate = qf.RN(theta, 0, 1, 0) assert qf.gates_close(gate, qf.RY(theta)) gate = qf.RN(theta, 0, 0, 1) assert qf.gates_close(gate, qf.RZ(theta)) gate = qf.RN(pi, np.sqrt(2), 0, np.sqrt(2)) assert qf.gates_close(gate, qf.H())
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 swap_test(ket, q0: qf.Qubit, q1: qf.Qubit, q2: qf.Qubit) -> qf.QubitTensor: """ Apply a Swap Test to measure fidelity between qubits q1 and q2. Qubit q0 is an ancilla, which should be zero state initially. The qubits cannot be initially entangled. """ circ = qf.Circuit() circ += qf.H(q0) circ += qf.CSwap(q0, q1, q2) circ += qf.H(q0) circ += qf.P0(q0) # Measure ket = circ.run(ket) fid = 2 * ket.norm() - 1.0 # return fidelity return fid
def test_sample() -> None: ket = qf.zero_state(2) ket = qf.H(0).run(ket) ket = qf.CNot(0, 1).run(ket) samples = ket.sample(10) assert samples.sum() == 10
def test_expectation(): ket = qf.zero_state(1) ket = qf.H(0).run(ket) m = ket.expectation([0.4, 0.6]) assert qf.asarray(m) - 0.5 == ALMOST_ZERO m = ket.expectation([0.4, 0.6], 10)
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_expectation() -> None: ket = qf.zero_state(1) ket = qf.H(0).run(ket) m = ket.expectation([0.4, 0.6]) assert np.isclose(m, 0.5) ket.expectation([0.4, 0.6], 10)
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_hadamard(): gate = qf.I() gate = qf.RZ(pi / 2, 0) @ gate gate = qf.RX(pi / 2, 0) @ gate gate = qf.RZ(pi / 2, 0) @ gate res = qf.asarray(qf.inner_product(gate.vec, qf.H().vec)) assert abs(res) / 2 == ALMOST_ONE