def test_ilo() -> None: b = AerBackend() bs = AerStateBackend() bu = AerUnitaryBackend() c = Circuit(2) c.X(1) assert (bs.get_state(c) == np.asarray([0, 1, 0, 0])).all() assert (bs.get_state(c, basis=BasisOrder.dlo) == np.asarray([0, 0, 1, 0])).all() assert (bu.get_unitary(c) == np.asarray([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])).all() assert (bu.get_unitary(c, basis=BasisOrder.dlo) == np.asarray([[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]])).all() c.measure_all() assert (b.get_shots(c, 2) == np.asarray([[0, 1], [0, 1]])).all() assert (b.get_shots(c, 2, basis=BasisOrder.dlo) == np.asarray([[1, 0], [1, 0]])).all() assert b.get_counts(c, 2) == {(0, 1): 2} assert b.get_counts(c, 2, basis=BasisOrder.dlo) == {(1, 0): 2}
def test_routing_measurements() -> None: qc = get_test_circuit(True) circ = qiskit_to_tk(qc) sim = AerBackend() original_results = sim.get_shots(circ, 10, seed=4) coupling = [[1, 0], [2, 0], [2, 1], [3, 2], [3, 4], [4, 2]] arc = Architecture(coupling) physical_c = route(circ, arc) Transform.DecomposeSWAPtoCX().apply(physical_c) Transform.DecomposeCXDirected(arc).apply(physical_c) Transform.OptimisePostRouting().apply(physical_c) assert (sim.get_shots(physical_c, 10) == original_results).all()
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_shots_bits_edgecases(n_shots: int, n_bits: int) -> None: c = Circuit(n_bits, n_bits) aer_backend = AerBackend() # TODO TKET-813 add more shot based backends and move to integration tests h = aer_backend.process_circuit(c, n_shots) res = aer_backend.get_result(h) correct_shots = np.zeros((n_shots, n_bits), dtype=int) correct_shape = (n_shots, n_bits) correct_counts = Counter({(0, ) * n_bits: n_shots}) # BackendResult assert np.array_equal(res.get_shots(), correct_shots) assert res.get_shots().shape == correct_shape assert res.get_counts() == correct_counts # Direct assert np.array_equal(aer_backend.get_shots(c, n_shots), correct_shots) assert aer_backend.get_shots(c, n_shots).shape == correct_shape assert aer_backend.get_counts(c, n_shots) == correct_counts
def test_noise() -> 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) n_qbs = 5 c = Circuit(n_qbs, n_qbs) x_qbs = [2, 0, 4] for i in x_qbs: c.X(i) c.measure_all() b = AerBackend(noise_model) n_shots = 50 b.compile_circuit(c) shots = b.get_shots(c, n_shots, seed=4) zer_exp = [] one_exp = [] for i in range(n_qbs): expectation = np.sum(shots[:, i]) / n_shots if i in x_qbs: one_exp.append(expectation) else: zer_exp.append(expectation) assert min(one_exp) > max(zer_exp) c2 = (Circuit(4, 4).H(0).CX(0, 2).CX(3, 1).T(2).CX(0, 1).CX(0, 3).CX(2, 1).measure_all()) b.compile_circuit(c2) shots = b.get_shots(c2, 10, seed=5) assert shots.shape == (10, 4)
def test_measures() -> None: n_qbs = 12 c = Circuit(n_qbs, n_qbs) x_qbs = [2, 5, 7, 11] for i in x_qbs: c.X(i) c.measure_all() b = AerBackend() shots = b.get_shots(c, 10) all_ones = True all_zeros = True for i in x_qbs: all_ones = all_ones and bool(np.all(shots[:, i])) for i in range(n_qbs): if i not in x_qbs: all_zeros = all_zeros and (not np.any(shots[:, i])) assert all_ones assert all_zeros
def test_sim() -> None: c = circuit_gen(True) b = AerBackend() shots = b.get_shots(c, 1024) print(shots)