コード例 #1
0
def test_circuit_light_cone():
    from qibo import __version__
    nqubits = 10
    c = Circuit(nqubits)
    c.add(gates.RY(i, theta=0) for i in range(nqubits))
    c.add(gates.CZ(i, i + 1) for i in range(0, nqubits - 1, 2))
    c.add(gates.RY(i, theta=0) for i in range(nqubits))
    c.add(gates.CZ(i, i + 1) for i in range(1, nqubits - 1, 2))
    c.add(gates.CZ(0, nqubits - 1))
    sc, qubit_map = c.light_cone(4, 5)
    target_qasm = f"""// Generated by QIBO {__version__}
OPENQASM 2.0;
include "qelib1.inc";
qreg q[6];
ry(0) q[0];
ry(0) q[1];
ry(0) q[2];
ry(0) q[3];
ry(0) q[4];
ry(0) q[5];
cz q[0],q[1];
cz q[2],q[3];
cz q[4],q[5];
ry(0) q[1];
ry(0) q[2];
ry(0) q[3];
ry(0) q[4];
cz q[1],q[2];
cz q[3],q[4];"""
    assert qubit_map == {2: 0, 3: 1, 4: 2, 5: 3, 6: 4, 7: 5}
    assert sc.to_qasm() == target_qasm
コード例 #2
0
def test_parametrized_gate():
    c = Circuit(2)
    c.add(gates.Y(0))
    c.add(gates.RY(1, 0.1234))
    target = f"""// Generated by QIBO {__version__}
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
y q[0];
ry(0.1234) q[1];"""
    assert_strings_equal(c.to_qasm(), target)
コード例 #3
0
ファイル: test_abstract_gates.py プロジェクト: mlazzarin/qibo
def test_controlled_by():
    gate = gates.RX(0, 0.1234).controlled_by(1, 2, 3)
    assert gate.target_qubits == (0,)
    assert gate.control_qubits == (1, 2, 3)
    assert gate.is_controlled_by
    assert isinstance(gate, gates.RX)

    with pytest.raises(RuntimeError):
        gate = gates.CNOT(0, 1).controlled_by(2)

    gate = gates.RY(0, 0.1234)
    gate.nqubits = 5
    with pytest.raises(RuntimeError):
        gate = gate.controlled_by(2)
コード例 #4
0
def test_circuit_light_cone_controlled_by():
    c = Circuit(4)
    c.add(gates.RY(2, theta=0).controlled_by(0, 1))
    c.add(gates.RX(3, theta=0))
    sc, qubit_map = c.light_cone(3)
    assert qubit_map == {3: 0}
    assert sc.nqubits == 1
    assert len(sc.queue) == 1
    assert isinstance(sc.queue[0], gates.RX)
    sc, qubit_map = c.light_cone(2)
    assert qubit_map == {0: 0, 1: 1, 2: 2}
    assert sc.nqubits == 3
    assert len(sc.queue) == 1
    assert isinstance(sc.queue[0], gates.RY)
コード例 #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_crotations():
    c = Circuit(3)
    c.add(gates.RX(0, 0.1))
    c.add(gates.RZ(1, 0.4))
    c.add(gates.CRX(0, 2, 0.5))
    c.add(gates.RY(1, 0.3).controlled_by(2))
    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];
crx(0.5) q[0],q[2];
cry(0.3) 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()
コード例 #7
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")