def test_circuit_set_parameters_with_dictionary(trainable): """Check updating parameters of circuit with list.""" params = [0.123, 0.456, 0.789] c1 = Circuit(3) c1.add(gates.X(0)) c1.add(gates.X(2)) if trainable: c1.add(gates.U1(0, theta=0, trainable=trainable)) else: c1.add(gates.U1(0, theta=params[0], trainable=trainable)) c2 = Circuit(3) c2.add(gates.RZ(1, theta=0)) c2.add(gates.CZ(1, 2)) c2.add(gates.CU1(0, 2, theta=0)) c2.add(gates.H(2)) c = c1 + c2 if trainable: params_dict = {c.queue[i]: p for i, p in zip([2, 3, 5], params)} c.set_parameters(params_dict) assert c.queue[2].parameters == params[0] else: params_dict = {c.queue[3]: params[1], c.queue[5]: params[2]} c.set_parameters(params_dict) assert c.queue[3].parameters == params[1] assert c.queue[5].parameters == params[2] # test not passing all parametrized gates c.set_parameters({c.queue[5]: 0.7891}) if trainable: assert c.queue[2].parameters == params[0] assert c.queue[3].parameters == params[1] assert c.queue[5].parameters == 0.7891
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_cu1(): c = Circuit(2) c.add(gates.RX(0, 0.1234)) c.add(gates.RZ(1, 0.4321)) c.add(gates.CU1(0, 1, 0.567)) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; rx(0.1234) q[0]; rz(0.4321) q[1]; cu1(0.567) q[0],q[1];""" assert_strings_equal(c.to_qasm(), target)
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_ugates(): c = Circuit(3) c.add(gates.RX(0, 0.1)) c.add(gates.RZ(1, 0.4)) c.add(gates.U2(2, 0.5, 0.6)) c.add(gates.CU1(0, 1, 0.7)) c.add(gates.CU3(2, 1, 0.2, 0.3, 0.4)) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; rx(0.1) q[0]; rz(0.4) q[1]; u2(0.5, 0.6) q[2]; cu1(0.7) q[0],q[1]; cu3(0.2, 0.3, 0.4) q[2],q[1];""" assert_strings_equal(c.to_qasm(), target) c = Circuit(2) c.add(gates.CU2(0, 1, 0.1, 0.2)) with pytest.raises(ValueError): target = c.to_qasm()
def __new__(cls, q0, q1, theta, trainable=True): if K.custom_gates: return CU1(q0, q1, theta, trainable) else: from qibo.core import gates return gates.CU1(q0, q1, theta, trainable)