def test_circuit_draw_line_wrap(): """Test circuit text draw with line wrap.""" ref_line_wrap_50 = \ 'q0: ─H─U1─U1─U1─U1───────────────────────────x───I───f ...\n' \ 'q1: ───o──|──|──|──H─U1─U1─U1────────────────|─x─I───| ...\n' \ 'q2: ──────o──|──|────o──|──|──H─U1─U1────────|─|─────| ...\n' \ 'q3: ─────────o──|───────o──|────o──|──H─U1───|─x───M─| ...\n' \ 'q4: ────────────o──────────o───────o────o──H─x───────f ...\n' \ '\n' \ 'q0: ... ─o────gf───M─\n' \ 'q1: ... ─U3───|──o─M─\n' \ 'q2: ... ────X─gf─o─M─\n' \ 'q3: ... ────o────o───\n' \ 'q4: ... ────o────X───' ref_line_wrap_30 = \ 'q0: ─H─U1─U1─U1─U1──────────────── ...\n' \ 'q1: ───o──|──|──|──H─U1─U1─U1───── ...\n' \ 'q2: ──────o──|──|────o──|──|──H─U1 ...\n' \ 'q3: ─────────o──|───────o──|────o─ ...\n' \ 'q4: ────────────o──────────o────── ...\n' \ '\n' \ 'q0: ... ───────────x───I───f─o────gf── ...\n' \ 'q1: ... ───────────|─x─I───|─U3───|──o ...\n' \ 'q2: ... ─U1────────|─|─────|────X─gf─o ...\n' \ 'q3: ... ─|──H─U1───|─x───M─|────o────o ...\n' \ 'q4: ... ─o────o──H─x───────f────o────X ...\n' \ '\n' \ 'q0: ... ─M─\n' \ 'q1: ... ─M─\n' \ 'q2: ... ─M─\n' \ 'q3: ... ───\n' \ 'q4: ... ───' import numpy as np circuit = Circuit(5) for i1 in range(5): circuit.add(gates.H(i1)) for i2 in range(i1 + 1, 5): circuit.add(gates.CU1(i2, i1, theta=0)) circuit.add(gates.SWAP(0, 4)) circuit.add(gates.SWAP(1, 3)) circuit.add(gates.I(*range(2))) circuit.add(gates.M(3, collapse=True)) circuit.add(gates.fSim(0, 4, 0, 0)) circuit.add(gates.CU3(0, 1, 0, 0, 0)) circuit.add(gates.TOFFOLI(4, 3, 2)) circuit.add(gates.GeneralizedfSim(0, 2, np.eye(2), 0)) circuit.add(gates.X(4).controlled_by(1, 2, 3)) circuit.add(gates.M(*range(3))) assert circuit.draw(line_wrap=50) == ref_line_wrap_50 assert circuit.draw(line_wrap=30) == ref_line_wrap_30
def test_circuit_draw(): """Test circuit text draw.""" ref = 'q0: ─H─U1─U1─U1─U1───────────────────────────x───\n' \ 'q1: ───o──|──|──|──H─U1─U1─U1────────────────|─x─\n' \ 'q2: ──────o──|──|────o──|──|──H─U1─U1────────|─|─\n' \ 'q3: ─────────o──|───────o──|────o──|──H─U1───|─x─\n' \ 'q4: ────────────o──────────o───────o────o──H─x───' circuit = Circuit(5) for i1 in range(5): circuit.add(gates.H(i1)) for i2 in range(i1 + 1, 5): circuit.add(gates.CU1(i2, i1, theta=0)) circuit.add(gates.SWAP(0, 4)) circuit.add(gates.SWAP(1, 3)) assert circuit.draw() == ref
def test_gates_commute(): assert gates.H(0).commutes(gates.X(1)) assert gates.H(0).commutes(gates.H(1)) assert gates.H(0).commutes(gates.H(0)) assert not gates.H(0).commutes(gates.Y(0)) assert not gates.CNOT(0, 1).commutes(gates.SWAP(1, 2)) assert not gates.CNOT(0, 1).commutes(gates.H(1)) assert not gates.CNOT(0, 1).commutes(gates.Y(0).controlled_by(2)) assert not gates.CNOT(2, 3).commutes(gates.CNOT(3, 0)) assert gates.CNOT(0, 1).commutes(gates.Y(2).controlled_by(0))
def test_multiqubit_gates(): c = Circuit(2) c.add(gates.H(0)) c.add(gates.CNOT(0, 1)) c.add(gates.X(1)) c.add(gates.SWAP(0, 1)) c.add(gates.X(0).controlled_by(1)) # `controlled_by` here falls back to CNOT and should work target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; h q[0]; cx q[0],q[1]; x q[1]; swap q[0],q[1]; cx q[1],q[0];""" assert_strings_equal(c.to_qasm(), target)
def test_swap_init(): gate = gates.SWAP(4, 3) assert gate.target_qubits == (4, 3)