def test_customgate() -> None: a = Symbol("a") def_circ = Circuit(2) def_circ.CZ(0, 1) def_circ.Rx(a, 1) gate_def = CustomGateDef.define("MyCRx", def_circ, [a]) circ = Circuit(3) circ.Rx(0.1, 0) circ.Rx(0.4, 2) circ.add_custom_gate(gate_def, [0.2], [0, 1]) qc1 = tk_to_qiskit(circ) newcirc = qiskit_to_tk(qc1) print(repr(newcirc)) qc2 = tk_to_qiskit(newcirc) correct_circ = Circuit(3).Rx(0.1, 0).Rx(0.4, 2).CZ(0, 1).Rx(0.2, 1) correct_qc = tk_to_qiskit(correct_circ) backend = Aer.get_backend("statevector_simulator") states = [] for qc in (qc1, qc2, correct_qc): job = execute([qc], backend) states.append(job.result().get_statevector(qc)) assert compare_statevectors(states[0], states[1]) assert compare_statevectors(states[1], states[2])
def test_ilo(qvm: None, quilc: None) -> None: b = ForestBackend("9q-square") bs = ForestStateBackend() c = Circuit(2) c.CZ(0, 1) c.Rx(1.0, 1) assert np.allclose(bs.get_state(c), np.asarray([0, -1j, 0, 0])) assert np.allclose(bs.get_state(c, basis=BasisOrder.dlo), np.asarray([0, 0, -1j, 0])) c.rename_units({Qubit(0): Node(0), Qubit(1): Node(1)}) 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_statevector() -> None: b = AerStateBackend() circ = Circuit(3, name="test") circ.H(2) circ.X(0) circ.H(0) circ.CX(0, 1) circ.CZ(1, 2) circ.Sdg(0) circ.Tdg(1) circ.Z(1) circ.T(2) circ.Rx(0.3333, 1) circ.Rz(0.3333, 1) zxcirc = tk_to_pyzx(circ) assert zxcirc.name == circ.name b.compile_circuit(circ) state = b.get_state(circ) circ2 = pyzx_to_tk(zxcirc) assert circ2.name == circ.name b.compile_circuit(circ2) state2 = b.get_state(circ2) assert np.allclose(state, state2, atol=1e-10)
# The `get_circuit()` method is available for all box types, and returns a `Circuit` object. For example: print(pbox.get_circuit().get_commands()) # ## Circuit composition # Circuits can be composed either serially, whereby wires are joined together, or in parallel, using the `append()` command. # # For a simple illustration of serial composition, let's create two circuits with compatible set of wires, and append the second to the first: c = Circuit(2) c.CX(0, 1) c1 = Circuit(2) c1.CZ(1, 0) c.append(c1) print(c.get_commands()) # In the above example, there was a one-to-one match between the unit IDs in the two circuits, and they were matched up accordingly. The same applied with named unit IDs: x, y = Qubit("x"), Qubit("y") c = Circuit() c.add_qubit(x) c.add_qubit(y) c.CX(x, y) c1 = Circuit()