def test_incrementer() -> None: """ Simulate an 8-bit incrementer """ b = QsharpToffoliSimulatorBackend() c = Circuit(8) c.add_gate(OpType.CnX, [0, 1, 2, 3, 4, 5, 6, 7]) c.add_gate(OpType.CnX, [0, 1, 2, 3, 4, 5, 6]) c.add_gate(OpType.CnX, [0, 1, 2, 3, 4, 5]) c.add_gate(OpType.CnX, [0, 1, 2, 3, 4]) c.add_gate(OpType.CnX, [0, 1, 2, 3]) c.CCX(0, 1, 2) c.CX(0, 1) c.X(0) for x in [0, 23, 79, 198, 255]: # some arbitrary 8-bit numbers circ = Circuit(8) # prepare the state corresponding to x for i in range(8): if (x >> i) % 2 == 1: circ.X(i) # append the incrementer circ.add_circuit(c, list(range(8))) circ.measure_all() # run the simulator b.compile_circuit(circ) bits = b.get_shots(circ, 1)[0] # check the result for i in range(8): assert bits[i] == ((x + 1) >> i) % 2
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_handles() -> None: b = QsharpEstimatorBackend() c = Circuit(3) c.CCX(0, 1, 2) b.compile_circuit(c, 0) handle = b.process_circuits([c])[0] resources = b.get_resources(handle) assert resources["T"] >= 7
def test_ccx_resources() -> None: """ Resources of a CCX. """ b = QsharpEstimatorBackend() c = Circuit(3) c.CCX(0, 1, 2) b.compile_circuit(c, 0) resources = b.get_resources(c) assert resources["T"] >= 7
def test_handles() -> None: b = QsharpToffoliSimulatorBackend() c = Circuit(4) c.CX(0, 1) c.CCX(0, 1, 2) c.add_gate(OpType.CnX, [0, 1, 2, 3]) c.add_gate(OpType.noop, [2]) c.X(3) c.SWAP(1, 2) c.measure_all() b.compile_circuit(c) shots = b.get_shots(c, n_shots=2) assert all(shots[0] == shots[1])
def test_default_pass() -> None: b = QsharpToffoliSimulatorBackend() for ol in range(3): comp_pass = b.default_compilation_pass(ol) c = Circuit(4, 4) c.CX(0, 1) c.CCX(0, 1, 2) c.add_gate(OpType.CnX, [0, 1, 2, 3]) c.add_gate(OpType.noop, [2]) c.X(3) c.SWAP(1, 2) c.measure_all() comp_pass.apply(c) for pred in b.required_predicates: assert pred.verify(c)
def test_compile() -> None: """ Compile a circuit containing SWAPs and noops down to CnX's """ b = QsharpToffoliSimulatorBackend() c = Circuit(4) c.CX(0, 1) c.CCX(0, 1, 2) c.add_gate(OpType.CnX, [0, 1, 2, 3]) c.add_gate(OpType.noop, [2]) c.X(3) c.SWAP(1, 2) c.measure_all() b.compile_circuit(c) shots = b.get_shots(c, 2) assert all(shots[0] == shots[1])
def test_compilation_correctness() -> None: c = Circuit(5) c.H(0).H(1).H(2) c.CX(0, 1).CX(1, 2) c.Rx(0.25, 1).Ry(0.75, 1).Rz(0.5, 2) c.CCX(2, 1, 0) c.CY(1, 0).CY(2, 1) c.H(0).H(1).H(2) c.Rz(0.125, 0) c.X(1) c.Rz(0.125, 2).X(2).Rz(0.25, 2) c.SX(3).Rz(0.125, 3).SX(3) c.CX(0, 3).CX(0, 4) u_backend = AerUnitaryBackend() u = u_backend.get_unitary(c) ibm_backend = IBMQBackend("ibmq_santiago", hub="ibm-q", group="open", project="main") for ol in range(3): p = ibm_backend.default_compilation_pass(optimisation_level=ol) cu = CompilationUnit(c) p.apply(cu) c1 = cu.circuit compiled_u = u_backend.get_unitary(c1) # Adjust for placement imap = cu.initial_map fmap = cu.final_map c_idx = {c.qubits[i]: i for i in range(5)} c1_idx = {c1.qubits[i]: i for i in range(5)} ini = {c_idx[qb]: c1_idx[node] for qb, node in imap.items()} inv_fin = {c1_idx[node]: c_idx[qb] for qb, node in fmap.items()} m_ini = lift_perm(ini) m_inv_fin = lift_perm(inv_fin) assert compare_unitaries(u, m_inv_fin @ compiled_u @ m_ini)