def sq(a, b, c): circ = Circuit(1) if c != 0: circ.Rz(c, 0) if b != 0: circ.Rx(b, 0) if a != 0: circ.Rz(a, 0) return circ
def phi(qubits, alpha, n_qubits): c = Circuit(n_qubits) for i in range(len(qubits) - 1): c.CX(qubits[i], qubits[i + 1]) c.Rz(qubits[-1], alpha) for i in range(len(qubits) - 1): c.CX(qubits[-i - 2], qubits[-i - 1]) return c
def test_transpile_grid_circuit(self): c = Circuit() qb = pytket._tket.circuit.Qubit("grid", 0, 0) c.add_qubit(qb) c.Rz(1, qb) qsc = jaqal_circuit_from_tket_circuit(c) jcirc = CircuitBuilder() reg = jcirc.register("baseregister", 1) reg2 = jcirc.map("grid0_0", reg, slice(0, 1, 1)) block = jcirc.block() block.gate("prepare_all") block.gate("Rz", reg2[0], pi) block.gate("measure_all") self.assertEqual(generate_jaqal_program(jcirc.build()), generate_jaqal_program(qsc))
def test_transpile_1q_circuit(self): c = Circuit(1, 1) c.Rz(1, 0) c.add_barrier([0]) c.Measure(0, 0) qsc = jaqal_circuit_from_tket_circuit(c) jcirc = CircuitBuilder() reg = jcirc.register("baseregister", 1) reg2 = jcirc.map("q", reg, slice(0, 1, 1)) block = jcirc.block() block.gate("prepare_all") block.gate("Rz", reg2[0], pi) block = jcirc.block() block.gate("measure_all") self.assertEqual(generate_jaqal_program(jcirc.build()), generate_jaqal_program(qsc))
def add_operator_term(circuit: Circuit, term: QubitPauliString, angle: float): qubits = [] for q, p in term.to_dict().items(): if p != Pauli.I: qubits.append(q) if p == Pauli.X: circuit.H(q) elif p == Pauli.Y: circuit.V(q) for i in range(len(qubits) - 1): circuit.CX(i, i + 1) circuit.Rz(angle, len(qubits) - 1) for i in reversed(range(len(qubits) - 1)): circuit.CX(i, i + 1) for q, p in term.to_dict().items(): if p == Pauli.X: circuit.H(q) elif p == Pauli.Y: circuit.Vdg(q)
def pauli_evolution(pauli: List[Tuple[int, str]], coeff: complex, circ: Circuit): """Appends the evolution circuit corresponding to a given Pauli tensor Args: pauli: coeff (complex): circ (Circuit): """ # set up the correct basis all_qbs = list(zip(*pauli))[0] for qb_idx, p in pauli: if p == 'X': circ.H(qb_idx) elif p == 'Y': # angles in half-turns circ.Rx(qb_idx, 0.5) # cnot cascade cx_qb_pairs = list(zip(sorted(all_qbs)[:-1], sorted(all_qbs)[1:])) for pair in cx_qb_pairs: circ.CX(pair[0], pair[1]) # rotation (convert angle from radians to half-turns) circ.Rz(all_qbs[-1], (2 * coeff.imag) / PI) # reverse cascade and revert basis cx_qb_pairs = list(zip(sorted(all_qbs)[:-1], sorted(all_qbs)[1:])) for pair in reversed(cx_qb_pairs): circ.CX(pair[0], pair[1]) all_qbs = list(zip(*pauli))[0] for qb_idx, p in pauli: if p == 'X': circ.H(qb_idx) elif p == 'Y': circ.Rx(qb_idx, -0.5)
backend = AerBackend() backend.compile_circuit(c) handle = backend.process_circuit(c, n_shots=2000) counts = backend.get_result(handle).get_counts() print(counts) # ## Statevector simulator usage from pytket import Circuit from pytket.extensions.qiskit import AerStateBackend # Build a quantum state: c = Circuit(3) c.H(0).CX(0, 1) c.Rz(0.3, 0) c.Rz(-0.3, 1) c.Ry(0.8, 2) # Examine the statevector: backend = AerStateBackend() backend.compile_circuit(c) handle = backend.process_circuit(c) state = backend.get_result(handle).get_state() print(state) # ## Expectation value usage from pytket import Circuit, Qubit from pytket.extensions.qiskit import AerBackend, AerStateBackend