예제 #1
0
def test_parametrizedgates_class():
    from qibo.abstractions.circuit import _ParametrizedGates
    paramgates = _ParametrizedGates()
    paramgates.append(gates.RX(0, 0.1234))
    paramgates.append(gates.fSim(0, 1, 0.1234, 0.4321))
    assert len(paramgates.set) == 2
    assert paramgates.nparams == 3
예제 #2
0
def test_fsim_init():
    import numpy as np
    gate = gates.fSim(0, 1, 0.1234, 0.4321)
    assert gate.target_qubits == (0, 1)
    matrix = np.random.random((2, 2))
    gate = gates.GeneralizedfSim(0, 1, matrix, 0.4321)
    assert gate.target_qubits == (0, 1)
    assert gate.parameters == (matrix, 0.4321)
    matrix = np.random.random((3, 3))
    with pytest.raises(ValueError):
        gate = gates.GeneralizedfSim(0, 1, matrix, 0.4321)
예제 #3
0
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
예제 #4
0
def test_circuit_set_parameters_errors():
    """Check updating parameters errors."""
    c = Circuit(2)
    c.add(gates.RX(0, theta=0.789))
    c.add(gates.RX(1, theta=0.789))
    c.add(gates.fSim(0, 1, theta=0.123, phi=0.456))

    with pytest.raises(KeyError):
        c.set_parameters({gates.RX(0, theta=1.0): 0.568})
    with pytest.raises(ValueError):
        c.set_parameters([0.12586])
    with pytest.raises(TypeError):
        c.set_parameters({0.3568})
    with pytest.raises(ValueError):
        c.queue[2].parameters = [0.1234, 0.4321, 0.156]
예제 #5
0
def test_circuit_set_parameters_with_list(trainable):
    """Check updating parameters of circuit with list."""
    params = [0.123, 0.456, (0.789, 0.321)]

    c = Circuit(3)
    if trainable:
        c.add(gates.RX(0, theta=0, trainable=trainable))
    else:
        c.add(gates.RX(0, theta=params[0], trainable=trainable))
    c.add(gates.RY(1, theta=0))
    c.add(gates.CZ(1, 2))
    c.add(gates.fSim(0, 2, theta=0, phi=0))
    c.add(gates.H(2))
    if trainable:
        c.set_parameters(params)
        assert c.queue[0].parameters == params[0]
    else:
        c.set_parameters(params[1:])
    assert c.queue[1].parameters == params[1]
    assert c.queue[3].parameters == params[2]
예제 #6
0
def test_get_parameters(trainable, include_not_trainable, format):
    import numpy as np
    matrix = np.random.random((2, 2))
    c = Circuit(3)
    c.add(gates.RX(0, theta=0.123))
    c.add(gates.RY(1, theta=0.456, trainable=trainable))
    c.add(gates.CZ(1, 2))
    c.add(gates.Unitary(matrix, 2))
    c.add(gates.fSim(0, 2, theta=0.789, phi=0.987, trainable=trainable))
    c.add(gates.H(2))
    c.param_tensor_types = (np.ndarray, )
    params = c.get_parameters(format, include_not_trainable)
    if trainable or include_not_trainable:
        target_params = {
            "list": [0.123, 0.456, (0.789, 0.987)],
            "dict": {
                c.queue[0]: 0.123,
                c.queue[1]: 0.456,
                c.queue[4]: (0.789, 0.987)
            },
            "flatlist": [0.123, 0.456]
        }
        target_params["flatlist"].extend(list(matrix.ravel()))
        target_params["flatlist"].extend([0.789, 0.987])
    else:
        target_params = {
            "list": [0.123],
            "dict": {
                c.queue[0]: 0.123
            },
            "flatlist": [0.123]
        }
        target_params["flatlist"].extend(list(matrix.ravel()))
    if format == "list":
        i = len(target_params["list"]) // 2 + 1
        np.testing.assert_allclose(params.pop(i), matrix)
    elif format == "dict":
        np.testing.assert_allclose(params.pop(c.queue[3]), matrix)
    assert params == target_params[format]
    with pytest.raises(ValueError):
        c.get_parameters("test")