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_ibmq_emulator() -> None: b_emu = IBMQEmulatorBackend("ibmq_santiago", hub="ibm-q", group="open", project="main") assert b_emu._noise_model is not None b_ibm = b_emu._ibmq b_aer = AerBackend() for ol in range(3): comp_pass = b_emu.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c_cop = c.copy() comp_pass.apply(c_cop) c.measure_all() for bac in (b_emu, b_ibm): assert all(pred.verify(c_cop) for pred in bac.required_predicates) c_cop_2 = c.copy() b_aer.compile_circuit(c_cop_2, ol) if ol == 0: assert not all( pred.verify(c_cop_2) for pred in b_emu.required_predicates) circ = Circuit(2, 2).H(0).CX(0, 1).measure_all() b_emu.compile_circuit(circ) b_noi = AerBackend(noise_model=b_emu._noise_model) emu_shots = b_emu.get_shots(circ, 10, seed=10) aer_shots = b_noi.get_shots(circ, 10, seed=10) assert np.array_equal(emu_shots, aer_shots)
def test_default_pass() -> None: b = ProjectQBackend() for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_default_pass() -> None: b = AQTBackend("invalid", device_name="sim/noise-model-1") for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_default_pass() -> None: b = HoneywellBackend(device_name="HQS-LT-1.0-APIVAL") for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_default_pass() -> None: b = QsharpSimulatorBackend() for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(4, 4) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_default_pass() -> None: b = IBMQBackend("ibmq_santiago", hub="ibm-q", group="open", project="main") for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_default_pass() -> None: b = ForestBackend("9q-square") for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_aer_default_pass() -> None: with open(os.path.join(sys.path[0], "ibmqx2_properties.pickle"), "rb") as f: properties = pickle.load(f) noise_model = NoiseModel.from_backend(properties) for nm in [None, noise_model]: b = AerBackend(nm) for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(3, 3) c.H(0) c.CX(0, 1) c.CSWAP(1, 0, 2) c.ZZPhase(0.84, 2, 0) c.add_gate(OpType.TK1, [0.2, 0.3, 0.4], [0]) comp_pass.apply(c) c.measure_all() for pred in b.required_predicates: assert pred.verify(c)
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
# ## 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()) # ## Predicates