def test_honeywell() -> None: token = os.getenv("HQS_AUTH") backend = HoneywellBackend( device_name="HQS-LT-1.0-APIVAL", machine_debug=skip_remote_tests ) c = Circuit(4, 4, "test 1") c.H(0) c.CX(0, 1) c.Rz(0.3, 2) c.CSWAP(0, 1, 2) c.CRz(0.4, 2, 3) c.CY(1, 3) c.ZZPhase(0.1, 2, 0) c.Tdg(3) c.measure_all() backend.compile_circuit(c) n_shots = 4 handle = backend.process_circuits([c], n_shots)[0] correct_shots = np.zeros((4, 4)) correct_counts = {(0, 0, 0, 0): 4} res = backend.get_result(handle, timeout=49) shots = res.get_shots() counts = res.get_counts() assert backend.circuit_status(handle).status is StatusEnum.COMPLETED assert np.all(shots == correct_shots) assert counts == correct_counts newshots = backend.get_shots(c, 4, timeout=49) assert np.all(newshots == correct_shots) newcounts = backend.get_counts(c, 4) assert newcounts == correct_counts if token is None: assert backend.device is None
def test_convert() -> None: circ = Circuit(4, 4) circ.H(0).CX(0, 1) circ.add_gate(OpType.noop, [1]) circ.CRz(0.5, 1, 2) circ.add_barrier([2]) circ.ZZPhase(0.3, 2, 3).CX(3, 0).Tdg(1) circ.Measure(0, 0) circ.Measure(1, 2) circ.Measure(2, 3) circ.Measure(3, 1) circ_aqt = tk_to_aqt(circ) assert json.loads(circ_aqt[1]) == [0, 3, 1, 2] assert all(gate[0] in ["X", "Y", "MS"] for gate in circ_aqt[0])
def test_convert() -> None: circ = Circuit(4) circ.H(0).CX(0, 1) circ.add_gate(OpType.noop, [1]) circ.CRz(0.5, 1, 2) circ.add_barrier([2]) circ.ZZPhase(0.3, 2, 3).CX(3, 0).Tdg(1) circ.measure_all() RebaseHQS().apply(circ) circ_hqs = circuit_to_qasm_str(circ, header="hqslib1") qasm_str = circ_hqs.split("\n")[6:-1] test = True for com in qasm_str: test &= any( com.startswith(gate) for gate in ("rz", "U1q", "ZZ", "measure", "barrier")) assert test
def test_aqt() -> None: # Run a circuit on the noisy simulator. token = cast(str, os.getenv("AQT_AUTH")) b = AQTBackend(token, device_name="sim/noise-model-1", label="test 1") c = Circuit(4, 4) c.H(0) c.CX(0, 1) c.Rz(0.3, 2) c.CSWAP(0, 1, 2) c.CRz(0.4, 2, 3) c.CY(1, 3) c.add_barrier([0, 1]) c.ZZPhase(0.1, 2, 0) c.Tdg(3) c.measure_all() b.compile_circuit(c) n_shots = 10 shots = b.get_shots(c, n_shots, seed=1, timeout=30) counts = b.get_counts(c, n_shots) assert len(shots) == n_shots assert sum(counts.values()) == n_shots
c.Rz(a + b, 0) c.symbol_substitution({b: 2 * a}) print(c.get_commands()) # ## Custom gates # We can define custom parametrized gates in `pytket` by first setting up a circuit containing symbolic parameters and then converting this to a parametrized operation type: from pytket.circuit import CustomGateDef a = Symbol("a") b = Symbol("b") setup = Circuit(3) setup.CX(0, 1) setup.Rz(a + 0.5, 2) setup.CRz(b, 0, 2) my_gate = CustomGateDef.define("g", setup, [a, b]) c = Circuit(4) c.add_custom_gate(my_gate, [0.2, 1.3], [0, 3, 1]) print(c.get_commands()) # Custom gates can also receive symbolic parameters: x = Symbol("x") c.add_custom_gate(my_gate, [x, 1.0], [0, 1, 2]) print(c.get_commands()) # ## Decomposing boxes and custom gates # Having defined a circuit containing custom gates, we may now want to decompose it into elementary gates. The `DecomposeBoxes()` transform allows us to do this:
# We will use Qiskit for circuit visualisation and for an example backend. For this it is necessary to have installed `pytket_qiskit` via `pip`. # # ## Passes # The basic mechanism of compilation is the 'pass', which is a transform that can be applied to a circuit. There is an extensive library of passes in `pytket`, and several standard ways in which they can be combined to form new passes. For example: from pytket.passes import DecomposeMultiQubitsIBM pass1 = DecomposeMultiQubitsIBM() # This pass converts all multi-qubit gates into CX and single-qubit gates. So let's create a circuit containing some non-CX multi-qubit gates: from pytket.circuit import Circuit circ = Circuit(3) circ.CRz(0.5, 0, 1) circ.T(2) circ.CSWAP(2, 0, 1) # In order to apply a pass to a circuit, we must first create a `CompilationUnit` from it. We can think of this as a 'bridge' between the circuit and the pass. The `CompilationUnit` is constructed from the circuit; the pass is applied to the `CompilationUnit`; and the transformed circuit is extracted from the `CompilationUnit`: from pytket.predicates import CompilationUnit cu = CompilationUnit(circ) pass1.apply(cu) circ1 = cu.circuit # Let's have a look at the result of the transformation: print(circ1.get_commands())