コード例 #1
0
ファイル: test_core_fusion.py プロジェクト: mlazzarin/qibo
def test_set_parameters_fusion(backend):
    """Check gate fusion when ``circuit.set_parameters`` is used."""
    c = Circuit(2)
    c.add(gates.RX(0, theta=0.1234))
    c.add(gates.RX(1, theta=0.1234))
    c.add(gates.CNOT(0, 1))
    c.add(gates.RY(0, theta=0.1234))
    c.add(gates.RY(1, theta=0.1234))
    fused_c = c.fuse()
    K.assert_allclose(fused_c(), c())

    c.set_parameters(4 * [0.4321])
    fused_c.set_parameters(4 * [0.4321])
    K.assert_allclose(fused_c(), c())
コード例 #2
0
def test_circuit_on_qubits_with_unitary_execution(backend, accelerators,
                                                  controlled):
    unitaries = np.random.random((2, 2, 2))
    smallc = Circuit(2)
    if controlled:
        smallc.add(gates.Unitary(unitaries[0], 0).controlled_by(1))
        smallc.add(gates.Unitary(unitaries[1], 1).controlled_by(0))
    else:
        smallc.add(gates.Unitary(unitaries[0], 0))
        smallc.add(gates.Unitary(unitaries[1], 1))
    smallc.add(gates.CNOT(0, 1))

    largec = Circuit(4, accelerators=accelerators)
    largec.add(gates.RY(0, theta=0.1))
    largec.add(gates.RY(1, theta=0.2))
    largec.add(gates.RY(2, theta=0.3))
    largec.add(gates.RY(3, theta=0.2))
    largec.add(smallc.on_qubits(3, 0))

    targetc = Circuit(4)
    targetc.add(gates.RY(0, theta=0.1))
    targetc.add(gates.RY(1, theta=0.2))
    targetc.add(gates.RY(2, theta=0.3))
    targetc.add(gates.RY(3, theta=0.2))
    if controlled:
        targetc.add(gates.Unitary(unitaries[0], 3).controlled_by(0))
        targetc.add(gates.Unitary(unitaries[1], 0).controlled_by(3))
    else:
        targetc.add(gates.Unitary(unitaries[0], 3))
        targetc.add(gates.Unitary(unitaries[1], 0))
    targetc.add(gates.CNOT(3, 0))
    assert largec.depth == targetc.depth
    K.assert_allclose(largec(), targetc())
コード例 #3
0
ファイル: test_variational.py プロジェクト: tuliplan/qibo
def test_vqe(method, options, compile, filename):
    """Performs a VQE circuit minimization test."""
    import qibo
    original_backend = qibo.get_backend()
    if method == "sgd" or compile:
        qibo.set_backend("matmuleinsum")
    else:
        qibo.set_backend("custom")

    original_threads = get_threads()
    if method == 'parallel_L-BFGS-B':
        if 'GPU' in qibo.get_device():  # pragma: no cover
            pytest.skip("unsupported configuration")
        qibo.set_threads(1)

    nqubits = 6
    layers = 4

    circuit = Circuit(nqubits)
    for l in range(layers):
        for q in range(nqubits):
            circuit.add(gates.RY(q, theta=1.0))
        for q in range(0, nqubits - 1, 2):
            circuit.add(gates.CZ(q, q + 1))
        for q in range(nqubits):
            circuit.add(gates.RY(q, theta=1.0))
        for q in range(1, nqubits - 2, 2):
            circuit.add(gates.CZ(q, q + 1))
        circuit.add(gates.CZ(0, nqubits - 1))
    for q in range(nqubits):
        circuit.add(gates.RY(q, theta=1.0))

    hamiltonian = XXZ(nqubits=nqubits)
    np.random.seed(0)
    initial_parameters = np.random.uniform(0, 2 * np.pi,
                                           2 * nqubits * layers + nqubits)
    v = VQE(circuit, hamiltonian)
    best, params = v.minimize(initial_parameters,
                              method=method,
                              options=options,
                              compile=compile)
    if method == "cma":
        # remove `outcmaes` folder
        import shutil
        shutil.rmtree("outcmaes")
    if filename is not None:
        utils.assert_regression_fixture(params, filename)
    qibo.set_backend(original_backend)
    qibo.set_threads(original_threads)
コード例 #4
0
ファイル: test_core_fusion.py プロジェクト: qiboteam/qibo
def test_variational_layer_fusion(backend, nqubits, nlayers, max_qubits):
    """Check fused variational layer execution."""
    theta = 2 * np.pi * np.random.random((2 * nlayers * nqubits,))
    theta_iter = iter(theta)

    c = Circuit(nqubits)
    for _ in range(nlayers):
        c.add((gates.RY(i, next(theta_iter)) 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, next(theta_iter)) 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))

    fused_c = c.fuse(max_qubits=max_qubits)
    K.assert_allclose(fused_c(), c())
コード例 #5
0
def test_construct_unitary_controlled(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234
    rotation = np.array([[np.cos(theta / 2.0), -np.sin(theta / 2.0)],
                         [np.sin(theta / 2.0), np.cos(theta / 2.0)]])
    target_matrix = np.eye(4, dtype=rotation.dtype)
    target_matrix[2:, 2:] = rotation
    gate = gates.RY(0, theta).controlled_by(1)
    np.testing.assert_allclose(gate.unitary, target_matrix)

    gate = gates.RY(0, theta).controlled_by(1, 2)
    with pytest.raises(NotImplementedError):
        unitary = gate.unitary
    qibo.set_backend(original_backend)
コード例 #6
0
def test_vqe(backend, method, options, compile, filename):
    """Performs a VQE circuit minimization test."""
    original_threads = qibo.get_threads()
    if (method == "sgd" or compile) and qibo.get_backend() != "tensorflow":
        pytest.skip("Skipping SGD test for unsupported backend.")

    if method == 'parallel_L-BFGS-B':
        device = qibo.get_device()
        backend = qibo.get_backend()
        if backend == "tensorflow" or backend == "qibojit" or "GPU" in device:
            pytest.skip("unsupported configuration")
        import sys
        if sys.platform == 'win32' or sys.platform == 'darwin':  # pragma: no cover
            pytest.skip("Parallel L-BFGS-B only supported on linux.")
        qibo.set_threads(1)

    nqubits = 6
    layers = 4
    circuit = models.Circuit(nqubits)
    for l in range(layers):
        for q in range(nqubits):
            circuit.add(gates.RY(q, theta=1.0))
        for q in range(0, nqubits - 1, 2):
            circuit.add(gates.CZ(q, q + 1))
        for q in range(nqubits):
            circuit.add(gates.RY(q, theta=1.0))
        for q in range(1, nqubits - 2, 2):
            circuit.add(gates.CZ(q, q + 1))
        circuit.add(gates.CZ(0, nqubits - 1))
    for q in range(nqubits):
        circuit.add(gates.RY(q, theta=1.0))

    hamiltonian = hamiltonians.XXZ(nqubits=nqubits)
    np.random.seed(0)
    initial_parameters = np.random.uniform(0, 2 * np.pi,
                                           2 * nqubits * layers + nqubits)
    v = models.VQE(circuit, hamiltonian)
    best, params, _ = v.minimize(initial_parameters,
                                 method=method,
                                 options=options,
                                 compile=compile)
    if method == "cma":
        # remove `outcmaes` folder
        import shutil
        shutil.rmtree("outcmaes")
    if filename is not None:
        assert_regression_fixture(params, filename)
    qibo.set_threads(original_threads)
コード例 #7
0
def test_vqe(backend, method, options, compile, filename, skip_parallel):
    """Performs a VQE circuit minimization test."""
    original_threads = qibo.get_threads()
    if (method == "sgd" or compile) and qibo.get_backend() != "tensorflow":
        pytest.skip("Skipping SGD test for unsupported backend.")

    if method == 'parallel_L-BFGS-B':  # pragma: no cover
        if skip_parallel:
            pytest.skip("Skipping parallel test.")
        from qibo.tests.test_parallel import is_parallel_supported
        backend_name = qibo.get_backend()
        if not is_parallel_supported(backend_name):
            pytest.skip(
                "Skipping parallel test due to unsupported configuration.")
        qibo.set_threads(1)

    nqubits = 6
    layers = 4
    circuit = models.Circuit(nqubits)
    for l in range(layers):
        for q in range(nqubits):
            circuit.add(gates.RY(q, theta=1.0))
        for q in range(0, nqubits - 1, 2):
            circuit.add(gates.CZ(q, q + 1))
        for q in range(nqubits):
            circuit.add(gates.RY(q, theta=1.0))
        for q in range(1, nqubits - 2, 2):
            circuit.add(gates.CZ(q, q + 1))
        circuit.add(gates.CZ(0, nqubits - 1))
    for q in range(nqubits):
        circuit.add(gates.RY(q, theta=1.0))

    hamiltonian = hamiltonians.XXZ(nqubits=nqubits)
    np.random.seed(0)
    initial_parameters = np.random.uniform(0, 2 * np.pi,
                                           2 * nqubits * layers + nqubits)
    v = models.VQE(circuit, hamiltonian)
    best, params, _ = v.minimize(initial_parameters,
                                 method=method,
                                 options=options,
                                 compile=compile)
    if method == "cma":
        # remove `outcmaes` folder
        import shutil
        shutil.rmtree("outcmaes")
    if filename is not None:
        assert_regression_fixture(params, filename)
    qibo.set_threads(original_threads)
コード例 #8
0
def test_vqe_custom_gates_errors():
    """Check that ``RuntimeError``s is raised when using custom gates."""
    import qibo
    original_backend = qibo.get_backend()
    qibo.set_backend("custom")

    nqubits = 6
    circuit = Circuit(nqubits)
    for q in range(nqubits):
        circuit.add(gates.RY(q, theta=0))
    for q in range(0, nqubits - 1, 2):
        circuit.add(gates.CZ(q, q + 1))

    hamiltonian = XXZ(nqubits=nqubits)
    initial_parameters = np.random.uniform(0, 2 * np.pi, 2 * nqubits + nqubits)
    v = VQE(circuit, hamiltonian)
    # compile with custom gates
    with pytest.raises(RuntimeError):
        best, params = v.minimize(initial_parameters,
                                  method="BFGS",
                                  options={'maxiter': 1},
                                  compile=True)
    # use SGD with custom gates
    with pytest.raises(RuntimeError):
        best, params = v.minimize(initial_parameters,
                                  method="sgd",
                                  compile=False)
    qibo.set_backend(original_backend)
コード例 #9
0
def test_two_variables_backpropagation(backend):
    if backend == "custom":
        pytest.skip("Custom gates do not support automatic differentiation.")
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    if K.name != "tensorflow":
        qibo.set_backend(original_backend)
        pytest.skip("Backpropagation is not supported by {}.".format(K.name))

    theta = K.optimization.Variable([0.1234, 0.4321], dtype=K.dtypes('DTYPE'))
    # TODO: Fix parametrized gates so that `Circuit` can be defined outside
    # of the gradient tape
    with K.optimization.GradientTape() as tape:
        c = Circuit(2)
        c.add(gates.RX(0, theta[0]))
        c.add(gates.RY(1, theta[1]))
        loss = K.real(c()[0])
    grad = tape.gradient(loss, theta)

    t = np.array([0.1234, 0.4321]) / 2.0
    target_loss = np.cos(t[0]) * np.cos(t[1])
    np.testing.assert_allclose(loss, target_loss)

    target_grad1 = -np.sin(t[0]) * np.cos(t[1])
    target_grad2 = -np.cos(t[0]) * np.sin(t[1])
    target_grad = np.array([target_grad1, target_grad2]) / 2.0
    np.testing.assert_allclose(grad, target_grad)
    qibo.set_backend(original_backend)
コード例 #10
0
def test_get_parameters():
    c = Circuit(3)
    c.add(gates.RX(0, theta=0.123))
    c.add(gates.RY(1, theta=0.456))
    c.add(gates.CZ(1, 2))
    c.add(gates.fSim(0, 2, theta=0.789, phi=0.987))
    c.add(gates.H(2))
    unitary = np.array([[0.123, 0.123], [0.123, 0.123]])
    c.add(gates.Unitary(unitary, 1))

    params = [0.123, 0.456, (0.789, 0.987), unitary]
    assert params == c.get_parameters()
    params = [0.123, 0.456, (0.789, 0.987), unitary]
    assert params == c.get_parameters()
    params = {
        c.queue[0]: 0.123,
        c.queue[1]: 0.456,
        c.queue[3]: (0.789, 0.987),
        c.queue[5]: unitary
    }
    assert params == c.get_parameters("dict")
    params = [0.123, 0.456, 0.789, 0.987]
    params.extend(unitary.ravel())
    assert params == c.get_parameters("flatlist")
    with pytest.raises(ValueError):
        c.get_parameters("test")
コード例 #11
0
ファイル: test_parametrized.py プロジェクト: tuliplan/qibo
def test_set_parameters_with_variationallayer(backend, accelerators, nqubits):
    """Check updating parameters of variational layer."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)

    theta = np.random.random(nqubits)
    c = Circuit(nqubits, accelerators)
    pairs = [(i, i + 1) for i in range(0, nqubits - 1, 2)]
    c.add(
        gates.VariationalLayer(range(nqubits), pairs, gates.RY, gates.CZ,
                               theta))

    target_c = Circuit(nqubits)
    target_c.add((gates.RY(i, theta[i]) for i in range(nqubits)))
    target_c.add((gates.CZ(i, i + 1) for i in range(0, nqubits - 1, 2)))
    np.testing.assert_allclose(c(), target_c())

    # Test setting VariationalLayer using a list
    new_theta = np.random.random(nqubits)
    c.set_parameters([np.copy(new_theta)])
    target_c.set_parameters(np.copy(new_theta))
    np.testing.assert_allclose(c(), target_c())

    # Test setting VariationalLayer using an array
    new_theta = np.random.random(nqubits)
    c.set_parameters(np.copy(new_theta))
    target_c.set_parameters(np.copy(new_theta))
    np.testing.assert_allclose(c(), target_c())

    qibo.set_backend(original_backend)
コード例 #12
0
ファイル: test_gates.py プロジェクト: hixio-mh/qibo
def test_unitary_common_gates(backend):
    """Check that `Unitary` gate can create common gates."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    c = Circuit(2)
    c.add(gates.X(0))
    c.add(gates.H(1))
    target_state = c.execute().numpy()
    c = Circuit(2)
    c.add(gates.Unitary(np.array([[0, 1], [1, 0]]), 0))
    c.add(gates.Unitary(np.array([[1, 1], [1, -1]]) / np.sqrt(2), 1))
    final_state = c.execute().numpy()
    np.testing.assert_allclose(final_state, target_state)

    thetax = 0.1234
    thetay = 0.4321
    c = Circuit(2)
    c.add(gates.RX(0, theta=thetax))
    c.add(gates.RY(1, theta=thetay))
    c.add(gates.CNOT(0, 1))
    target_state = c.execute().numpy()
    c = Circuit(2)
    rx = np.array([[np.cos(thetax / 2), -1j * np.sin(thetax / 2)],
                   [-1j * np.sin(thetax / 2), np.cos(thetax / 2)]])
    ry = np.array([[np.cos(thetay / 2), -np.sin(thetay / 2)],
                   [np.sin(thetay / 2), np.cos(thetay / 2)]])
    cnot = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
    c.add(gates.Unitary(rx, 0))
    c.add(gates.Unitary(ry, 1))
    c.add(gates.Unitary(cnot, 0, 1))
    final_state = c.execute().numpy()
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
コード例 #13
0
def test_unitary_common_gates(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    target_state = apply_gates([gates.X(0), gates.H(1)], nqubits=2)
    gatelist = [gates.Unitary(np.array([[0, 1], [1, 0]]), 0),
                gates.Unitary(np.array([[1, 1], [1, -1]]) / np.sqrt(2), 1)]
    final_state = apply_gates(gatelist, nqubits=2)
    np.testing.assert_allclose(final_state, target_state)

    thetax = 0.1234
    thetay = 0.4321
    gatelist = [gates.RX(0, theta=thetax), gates.RY(1, theta=thetay),
                gates.CNOT(0, 1)]
    target_state = apply_gates(gatelist, nqubits=2)

    rx = np.array([[np.cos(thetax / 2), -1j * np.sin(thetax / 2)],
                   [-1j * np.sin(thetax / 2), np.cos(thetax / 2)]])
    ry = np.array([[np.cos(thetay / 2), -np.sin(thetay / 2)],
                   [np.sin(thetay / 2), np.cos(thetay / 2)]])
    cnot = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
    gatelist = [gates.Unitary(rx, 0), gates.Unitary(ry, 1),
                gates.Unitary(cnot, 0, 1)]
    final_state = apply_gates(gatelist, nqubits=2)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
コード例 #14
0
def VariationalCircuit(nqubits, nlayers=1, theta_values=None):
    if theta_values is None:
        theta = iter(2 * np.pi * np.random.random(nlayers * 2 * nqubits))
    else:
        theta = iter(theta_values)

    for l in range(nlayers):
        for i in range(nqubits):
            yield gates.RY(i, next(theta))
        for i in range(0, nqubits - 1, 2):
            yield gates.CZ(i, i + 1)
        for i in range(nqubits):
            yield gates.RY(i, next(theta))
        for i in range(1, nqubits - 2, 2):
          yield gates.CZ(i, i + 1)
        yield gates.CZ(0, nqubits - 1)
コード例 #15
0
ファイル: qlassifier.py プロジェクト: tuliplan/qibo
 def _initialize_circuit(self):
     """Creates variational circuit."""
     C = Circuit(1)
     for l in range(self.layers):
         C.add(gates.RY(0, theta=0))
         C.add(gates.RZ(0, theta=0))
     return C
コード例 #16
0
def test_two_variables_backpropagation(backend):
    """Check that backpropagation works when using `tf.Variable` parameters."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    import tensorflow as tf
    from qibo.config import DTYPES
    theta = tf.Variable([0.1234, 0.4321], dtype=DTYPES.get('DTYPE'))

    # TODO: Fix parametrized gates so that `Circuit` can be defined outside
    # of the gradient tape
    with tf.GradientTape() as tape:
        c = Circuit(2)
        c.add(gates.RX(0, theta[0]))
        c.add(gates.RY(1, theta[1]))
        loss = tf.math.real(c()[0])
    grad = tape.gradient(loss, theta)

    t = np.array([0.1234, 0.4321]) / 2.0
    target_loss = np.cos(t[0]) * np.cos(t[1])
    np.testing.assert_allclose(loss.numpy(), target_loss)

    target_grad1 = -np.sin(t[0]) * np.cos(t[1])
    target_grad2 = -np.cos(t[0]) * np.sin(t[1])
    target_grad = np.array([target_grad1, target_grad2]) / 2.0
    np.testing.assert_allclose(grad.numpy(), target_grad)
    qibo.set_backend(original_backend)
コード例 #17
0
def test_vqe_custom_gates_errors():
    """Check that ``RuntimeError``s is raised when using custom gates."""
    if "qibotf" not in qibo.K.available_backends:  # pragma: no cover
        pytest.skip("Custom backend not available.")

    original_backend = qibo.get_backend()
    qibo.set_backend("qibotf")

    nqubits = 6
    circuit = models.Circuit(nqubits)
    for q in range(nqubits):
        circuit.add(gates.RY(q, theta=0))
    for q in range(0, nqubits - 1, 2):
        circuit.add(gates.CZ(q, q + 1))

    hamiltonian = hamiltonians.XXZ(nqubits=nqubits)
    initial_parameters = np.random.uniform(0, 2 * np.pi, 2 * nqubits + nqubits)
    v = models.VQE(circuit, hamiltonian)
    # compile with custom gates
    with pytest.raises(RuntimeError):
        best, params, _ = v.minimize(initial_parameters,
                                     method="BFGS",
                                     options={'maxiter': 1},
                                     compile=True)
    # use SGD with custom gates
    with pytest.raises(RuntimeError):
        best, params, _ = v.minimize(initial_parameters,
                                     method="sgd",
                                     compile=False)
    qibo.set_backend(original_backend)
コード例 #18
0
ファイル: test_core_circuit.py プロジェクト: mlazzarin/qibo
def test_repeated_execute(backend, accelerators):
    c = Circuit(4, accelerators)
    thetas = np.random.random(4)
    c.add((gates.RY(i, t) for i, t in enumerate(thetas)))
    c.repeated_execution = True
    target_state = np.array(20 * [c()])
    final_state = c(nshots=20)
    K.assert_allclose(final_state, target_state)
コード例 #19
0
def test_ry(backend):
    theta = 0.1234
    final_state = apply_gates(
        [gates.H(0), gates.RY(0, theta=theta)], nqubits=1)
    phase = np.exp(1j * theta / 2.0)
    gate = np.array([[phase.real, -phase.imag], [phase.imag, phase.real]])
    target_state = gate.dot(np.ones(2)) / np.sqrt(2)
    K.assert_allclose(final_state, target_state)
コード例 #20
0
def test_controlled_swap(backend, applyx, free_qubit):
    f = int(free_qubit)
    c = Circuit(3 + f)
    if applyx:
        c.add(gates.X(0))
    c.add(gates.RX(1 + f, theta=0.1234))
    c.add(gates.RY(2 + f, theta=0.4321))
    c.add(gates.SWAP(1 + f, 2 + f).controlled_by(0))
    final_state = c.execute()
    c = Circuit(3 + f)
    c.add(gates.RX(1 + f, theta=0.1234))
    c.add(gates.RY(2 + f, theta=0.4321))
    if applyx:
        c.add(gates.X(0))
        c.add(gates.SWAP(1 + f, 2 + f))
    target_state = c.execute()
    K.assert_allclose(final_state, target_state)
コード例 #21
0
def test_variable_theta(backend):
    """Check that parametrized gates accept `tf.Variable` parameters."""
    backend = qibo.get_backend()
    if backend != "tensorflow":
        pytest.skip("Numpy backends do not support variable parameters.")
    theta1 = K.optimization.Variable(0.1234, dtype=K.dtypes('DTYPE'))
    theta2 = K.optimization.Variable(0.4321, dtype=K.dtypes('DTYPE'))
    cvar = Circuit(2)
    cvar.add(gates.RX(0, theta1))
    cvar.add(gates.RY(1, theta2))
    final_state = cvar()

    c = Circuit(2)
    c.add(gates.RX(0, 0.1234))
    c.add(gates.RY(1, 0.4321))
    target_state = c()
    K.assert_allclose(final_state, target_state)
コード例 #22
0
ファイル: test_qasm.py プロジェクト: tuliplan/qibo
def test_crotations_cirq():
    c1 = Circuit(3)
    c1.add(gates.RX(0, 0.1))
    c1.add(gates.RZ(1, 0.4))
    c1.add(gates.CRX(0, 2, 0.5))
    c1.add(gates.RY(1, 0.3).controlled_by(2))
    # catches unknown gate "crx"
    with pytest.raises(exception.QasmException):
        c2 = circuit_from_qasm(c1.to_qasm())
コード例 #23
0
def test_circuit_on_qubits_execution(backend, accelerators, distribute_small):
    if distribute_small:
        smallc = Circuit(3, accelerators=accelerators)
    else:
        smallc = Circuit(3)
    smallc.add((gates.RX(i, theta=i + 0.1) for i in range(3)))
    smallc.add((gates.CNOT(0, 1), gates.CZ(1, 2)))

    largec = Circuit(6, accelerators=accelerators)
    largec.add((gates.RY(i, theta=i + 0.2) for i in range(0, 6, 2)))
    largec.add(smallc.on_qubits(1, 3, 5))

    targetc = Circuit(6)
    targetc.add((gates.RY(i, theta=i + 0.2) for i in range(0, 6, 2)))
    targetc.add((gates.RX(i, theta=i // 2 + 0.1) for i in range(1, 6, 2)))
    targetc.add((gates.CNOT(1, 3), gates.CZ(3, 5)))
    assert largec.depth == targetc.depth
    K.assert_allclose(largec(), targetc())
コード例 #24
0
def test_circuit_decompose_execution(backend):
    c = Circuit(6)
    c.add(gates.RX(0, 0.1234))
    c.add(gates.RY(1, 0.4321))
    c.add((gates.H(i) for i in range(2, 6)))
    c.add(gates.CNOT(0, 1))
    c.add(gates.X(3).controlled_by(0, 1, 2, 4))
    decomp_c = c.decompose(5)
    K.assert_allclose(c(), decomp_c(), atol=1e-6)
コード例 #25
0
def test_controlled_swap_double(backend, applyx):
    c = Circuit(4)
    c.add(gates.X(0))
    if applyx:
        c.add(gates.X(3))
    c.add(gates.RX(1, theta=0.1234))
    c.add(gates.RY(2, theta=0.4321))
    c.add(gates.SWAP(1, 2).controlled_by(0, 3))
    c.add(gates.X(0))
    final_state = c.execute()
    c = Circuit(4)
    c.add(gates.RX(1, theta=0.1234))
    c.add(gates.RY(2, theta=0.4321))
    if applyx:
        c.add(gates.X(3))
        c.add(gates.SWAP(1, 2))
    target_state = c.execute()
    K.assert_allclose(final_state, target_state)
コード例 #26
0
ファイル: test_parametrized.py プロジェクト: tuliplan/qibo
def test_circuit_set_parameters_with_list(backend, accelerators, trainable):
    """Check updating parameters of circuit with list."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    params = [0.123, 0.456, (0.789, 0.321)]

    c = Circuit(3, accelerators)
    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))
    # execute once
    final_state = c()

    target_c = Circuit(3)
    target_c.add(gates.RX(0, theta=params[0]))
    target_c.add(gates.RY(1, theta=params[1]))
    target_c.add(gates.CZ(1, 2))
    target_c.add(gates.fSim(0, 2, theta=params[2][0], phi=params[2][1]))
    target_c.add(gates.H(2))

    if trainable:
        c.set_parameters(params)
    else:
        c.set_parameters(params[1:])
    np.testing.assert_allclose(c(), target_c())

    # Attempt using a flat np.ndarray/list
    for new_params in (np.random.random(4), list(np.random.random(4))):
        if trainable:
            c.set_parameters(new_params)
        else:
            new_params[0] = params[0]
            c.set_parameters(new_params[1:])
        target_params = [
            new_params[0], new_params[1], (new_params[2], new_params[3])
        ]
        target_c.set_parameters(target_params)
        np.testing.assert_allclose(c(), target_c())
    qibo.set_backend(original_backend)
コード例 #27
0
def bell_circuit(basis):
    '''Create a Bell circuit with a distinct measurement basis and parametrizable gates.
    Args:
        basis (str): '00', '01, '10', '11' where a '1' marks a measurement in the X basis 
                    and a '0' a measurement in the Z basis.
    
    Returns:
        :class:`qibo.core.circuit.Circuit`
    
    '''
    c = Circuit(2)
    c.add(gates.RY(0, theta=0))
    c.add(gates.CNOT(0, 1))
    c.add(gates.RY(0, theta=0))
    for a in range(2):
        if basis[a] == '1':
            c.add(gates.H(a))
    c.add(gates.M(*range(2)))
    return c
コード例 #28
0
def test_measurement_result_parameters_random(backend, accelerators):
    test_device = K.cpu_devices[0] if accelerators else K.default_device
    initial_state = random_state(4)
    set_device_seed(123, accelerators)
    c = models.Circuit(4, accelerators)
    output = c.add(gates.M(1, collapse=True))
    c.add(gates.RY(0, theta=np.pi * output / 5))
    c.add(gates.RX(2, theta=np.pi * output / 4))
    result = c(initial_state=np.copy(initial_state))
    assert len(output.frequencies()) == 1

    set_device_seed(123, accelerators)
    with K.device(test_device):
        collapse = gates.M(1, collapse=True)
        target_state = collapse(K.cast(np.copy(initial_state)))
        if int(collapse.result.outcome()):
            target_state = gates.RY(0, theta=np.pi / 5)(target_state)
            target_state = gates.RX(2, theta=np.pi / 4)(target_state)
    K.assert_allclose(result, target_state)
コード例 #29
0
def test_measurement_result_parameters_multiple_qubits(backend):
    initial_state = random_state(4)
    K.set_seed(123)
    c = models.Circuit(4)
    output = c.add(gates.M(0, 1, 2, collapse=True))
    c.add(gates.RY(1, theta=np.pi * output[0] / 5))
    c.add(gates.RX(3, theta=np.pi * output[2] / 3))
    result = c(initial_state=np.copy(initial_state))

    K.set_seed(123)
    collapse = gates.M(0, 1, 2, collapse=True)
    target_state = collapse(K.cast(np.copy(initial_state)))
    # not including in coverage because outcomes are probabilistic and may
    # not occur for the CI run
    if int(collapse.result.outcome(0)):  # pragma: no cover
        target_state = gates.RY(1, theta=np.pi / 5)(target_state)
    if int(collapse.result.outcome(2)):  # pragma: no cover
        target_state = gates.RX(3, theta=np.pi / 3)(target_state)
    K.assert_allclose(result, target_state)
コード例 #30
0
ファイル: test_core_fusion.py プロジェクト: qiboteam/qibo
def test_fuse_circuit_two_qubit_gates(backend):
    """Check circuit fusion in circuit with two-qubit gates only."""
    c = Circuit(2)
    c.add(gates.CNOT(0, 1))
    c.add(gates.RX(0, theta=0.1234).controlled_by(1))
    c.add(gates.SWAP(0, 1))
    c.add(gates.fSim(1, 0, theta=0.1234, phi=0.324))
    c.add(gates.RY(1, theta=0.1234).controlled_by(0))
    fused_c = c.fuse()
    K.assert_allclose(fused_c(), c())