def test_circuit_vs_gate_execution(backend, compile):
    """Check consistency between executing circuit and stand alone gates."""
    from qibo import K
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    theta = 0.1234
    target_c = Circuit(2)
    target_c.add(gates.X(0))
    target_c.add(gates.X(1))
    target_c.add(gates.CU1(0, 1, theta))
    target_result = target_c()

    # custom circuit
    def custom_circuit(initial_state, theta):
        l1 = gates.X(0)(initial_state)
        l2 = gates.X(1)(l1)
        o = gates.CU1(0, 1, theta)(l2)
        return o

    initial_state = target_c.get_initial_state()
    if compile:
        c = K.compile(custom_circuit)
    else:
        c = custom_circuit

    if backend == "custom" and compile:
        with pytest.raises(NotImplementedError):
            result = c(initial_state, theta)
    else:
        result = c(initial_state, theta)
        np.testing.assert_allclose(result, target_result)
    qibo.set_backend(original_backend)
Exemple #2
0
def test_circuit_vs_gate_execution(backend, compile):
    """Check consistency between executing circuit and stand alone gates."""
    from qibo import K
    theta = 0.1234
    target_c = Circuit(2)
    target_c.add(gates.X(0))
    target_c.add(gates.X(1))
    target_c.add(gates.CU1(0, 1, theta))
    target_result = target_c()

    # custom circuit
    def custom_circuit(initial_state, theta):
        l1 = gates.X(0)(initial_state)
        l2 = gates.X(1)(l1)
        o = gates.CU1(0, 1, theta)(l2)
        return o

    initial_state = target_c.get_initial_state()
    if compile:
        c = K.compile(custom_circuit)
    else:
        c = custom_circuit

    result = c(initial_state, theta)
    K.assert_allclose(result, target_result)
Exemple #3
0
def test_variational_layer_density_matrix(backend, nqubits):
    from qibo.models import Circuit
    theta = 2 * np.pi * np.random.random(nqubits)
    c = Circuit(nqubits, density_matrix=True)
    c.add((gates.RY(i, t) for i, t in enumerate(theta)))
    c.add((gates.CZ(i, i + 1) for i in range(0, nqubits - 1, 2)))
    target_state = c()
    pairs = list((i, i + 1) for i in range(0, nqubits - 1, 2))
    c = Circuit(nqubits, density_matrix=True)
    c.add(gates.VariationalLayer(range(nqubits), pairs,
                                  gates.RY, gates.CZ, theta))
    final_state = c()
    K.assert_allclose(target_state, final_state)
    gate = gates.VariationalLayer(range(nqubits), pairs,
                                  gates.RY, gates.CZ, theta)
    gate.density_matrix = True
    final_state = gate(c.get_initial_state())
    K.assert_allclose(target_state, final_state)
Exemple #4
0
def test_get_initial_state(backend):
    c = Circuit(2)
    final_state = c.get_initial_state()
    target_state = np.zeros(4)
    target_state[0] = 1
    K.assert_allclose(final_state, target_state)
    with pytest.raises(ValueError):
        state = c.get_initial_state(np.zeros(2**3))
    with pytest.raises(ValueError):
        final_state = c.get_initial_state(np.zeros((2, 2)))
    with pytest.raises(ValueError):
        final_state = c.get_initial_state(np.zeros((2, 2, 2)))
    with pytest.raises(TypeError):
        final_state = c.get_initial_state(0)
    c = Circuit(2)
    c.check_initial_state_shape = False
    with pytest.raises(TypeError):
        final_state = c.get_initial_state(0)