def test_estimates() -> None: """ Check that the resource estimator gives reasonable results. """ b = QsharpEstimatorBackend() c = Circuit(3) c.H(0) c.CX(0, 1) c.CCX(0, 1, 2) c.Rx(0.3, 1) c.Ry(0.4, 2) c.Rz(1.1, 0) c.S(1) c.SWAP(0, 2) c.T(1) c.X(0) c.Y(1) c.Z(2) pbox = PauliExpBox([Pauli.X, Pauli.I, Pauli.Z], 0.25) c.add_pauliexpbox(pbox, [2, 0, 1]) b.compile_circuit(c, 0) resources = b.get_resources(c) assert resources["CNOT"] >= 1 assert resources["QubitClifford"] >= 1 assert resources["R"] >= 1 assert resources["T"] >= 1 assert resources["Depth"] >= 1 assert resources["Width"] == 3 assert resources["BorrowedWidth"] == 0
def test_convert() -> None: c = Circuit(3) c.H(0) c.H(1) c.CX(1, 0) c.X(1) pbox = PauliExpBox([Pauli.X, Pauli.Z, Pauli.X], 0.25) c.add_pauliexpbox(pbox, [2, 0, 1]) qs = tk_to_qsharp(c) assert "H(q[1]);" in qs
def h2_JW_sto3g_ansatz(): symbols = [Symbol("s0"), Symbol("s1"), Symbol("s2")] ansatz = Circuit(4) # Initialise in Hartree-Fock state ansatz.X(0) ansatz.X(1) # Single excitations ansatz.add_pauliexpbox( PauliExpBox([Pauli.X, Pauli.Z, Pauli.Y, Pauli.I], -symbols[0]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.Y, Pauli.Z, Pauli.X, Pauli.I], symbols[0]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.I, Pauli.X, Pauli.Z, Pauli.Y], -symbols[1]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.I, Pauli.Y, Pauli.Z, Pauli.X], symbols[1]), [0, 1, 2, 3] ) # Double excitations ansatz.add_pauliexpbox( PauliExpBox([Pauli.X, Pauli.X, Pauli.X, Pauli.Y], -symbols[2]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.X, Pauli.X, Pauli.Y, Pauli.X], -symbols[2]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.X, Pauli.Y, Pauli.X, Pauli.X], symbols[2]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.Y, Pauli.X, Pauli.X, Pauli.X], symbols[2]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.X, Pauli.Y, Pauli.Y, Pauli.Y], -symbols[2]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.Y, Pauli.X, Pauli.Y, Pauli.Y], -symbols[2]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.Y, Pauli.Y, Pauli.X, Pauli.Y], symbols[2]), [0, 1, 2, 3] ) ansatz.add_pauliexpbox( PauliExpBox([Pauli.Y, Pauli.Y, Pauli.Y, Pauli.X], symbols[2]), [0, 1, 2, 3] ) # Synthesise structures into primitive gates DecomposeBoxes().apply(ansatz) return ansatz, symbols
# Add a `Unitary2qBox`: m2 = np.asarray([[0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0]]) m2box = Unitary2qBox(m2) boxycirc.add_unitary2qbox(m2box, 1, 2) # Add an `ExpBox`: A = np.asarray([[1, 2, 3, 4 + 1j], [2, 0, 1j, -1], [3, -1j, 2, 1j], [4 - 1j, -1, -1j, 1]]) ebox = ExpBox(A, 0.5) boxycirc.add_expbox(ebox, 0, 1) # Add a `PauliExpBox`: pbox = PauliExpBox([Pauli.X, Pauli.Z, Pauli.X], 0.75) boxycirc.add_pauliexpbox(pbox, [0, 1, 2]) print(boxycirc.get_commands()) # The `get_circuit()` method is available for all box types, and returns a `Circuit` object. For example: print(pbox.get_circuit().get_commands()) # ## Circuit composition # Circuits can be composed either serially, whereby wires are joined together, or in parallel, using the `append()` command. # # For a simple illustration of serial composition, let's create two circuits with compatible set of wires, and append the second to the first: c = Circuit(2)
def add_excitation(circ, term_dict, param): for term, coeff in term_dict.items(): qubits, paulis = zip(*term.to_dict().items()) pbox = PauliExpBox(paulis, coeff * param) circ.add_pauliexpbox(pbox, qubits)
step_size = 0.01 n_qubits = [5, 10, 20, 30, 40, 50] n_runs = 10 for nb_qubits in n_qubits: data = [] for run_id in range(n_runs): # Start timer start = time.time() circ = Circuit(nb_qubits) h = 1.0 Jz = 1.0 for i in range(nb_steps): # Using Heisenberg Hamiltonian: for q in range(nb_qubits): circ.add_pauliexpbox(PauliExpBox([Pauli.X], -h * step_size), [q]) for q in range(nb_qubits - 1): circ.add_pauliexpbox( PauliExpBox([Pauli.Z, Pauli.Z], -Jz * step_size), [q, q + 1]) # Compile to gates backend = AerStateBackend() circ = backend.get_compiled_circuit(circ) # Apply optimization FullPeepholeOptimise().apply(circ) end = time.time()