예제 #1
0
def test_grover_iterative(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)

    def check(result):
        for i in result:
            if int(i) != 1:
                return False
        return True
    
    def check_false(result):
        return False

    oracle = Circuit(5 + 1)
    oracle.add(gates.X(5).controlled_by(*range(5)))
    grover = Grover(oracle, superposition_qubits=5, check=None, iterative=True)
    with pytest.raises(ValueError):
        solution, iterations = grover()
    grover = Grover(oracle, superposition_qubits=5, check=check_false, iterative=True)
    with pytest.raises(TimeoutError):
        solution, iterations = grover()
    grover = Grover(oracle, superposition_qubits=5, check=check, iterative=True)
    solution, iterations = grover(logs=True)
    assert solution == "11111"
    qibo.set_backend(original_backend)
예제 #2
0
def test_grover_init_default_superposition(backend):
    oracle = Circuit(5 + 1)
    oracle.add(gates.X(5).controlled_by(*range(5)))
    # try to initialize without passing `superposition_qubits`
    with pytest.raises(ValueError):
        grover = Grover(oracle)

    grover = Grover(oracle, superposition_qubits=4)
    assert grover.oracle == oracle
    assert grover.sup_qubits == 4
    assert grover.sup_size == 16
    assert grover.superposition.depth == 1
    assert grover.superposition.ngates == 4
예제 #3
0
파일: example3.py 프로젝트: rickyHong/qibo
def main(nqubits, num_1):
    """Creates a superposition circuit that finds all states with num_1 1's in a
    fixed number of qubits, then the oracle find that state where all the 1's
    are at the beginning of the bitstring. This oracle has got ancillas

    Args:
        nqubits (int): number of qubits
        num_1 (int): number of 1's to find

    Returns:
        solution (str): found string
        iterations (int): number of iterations needed
    """
    superposition = superposition_circuit(nqubits, num_1)

    oracle_circuit = oracle(nqubits, num_1)
    or_circuit = Circuit(oracle_circuit.nqubits)
    or_circuit.add(
        oracle_circuit.on_qubits(
            *(list(range(nqubits)) + [oracle_circuit.nqubits - 1] +
              list(range(nqubits, oracle_circuit.nqubits - 1)))))

    grover = Grover(or_circuit,
                    superposition_circuit=superposition,
                    superposition_qubits=nqubits,
                    number_solutions=1,
                    superposition_size=int(binomial(nqubits, num_1)))

    solution, iterations = grover()

    print('The solution is', solution)
    print('Number of iterations needed:', iterations)

    return solution, iterations
예제 #4
0
def test_grover_target_amplitude(backend):
    oracle = Circuit(5 + 1)
    oracle.add(gates.X(5).controlled_by(*range(5)))
    grover = Grover(oracle,
                    superposition_qubits=5,
                    target_amplitude=1 / 2**(5 / 2))
    solution, iterations = grover(logs=True)
    assert len(solution) == 1
    assert solution == ['11111']
예제 #5
0
def test_grover_init(backend):
    oracle = Circuit(5 + 1)
    oracle.add(gates.X(5).controlled_by(*range(5)))
    superposition = Circuit(5)
    superposition.add([gates.H(i) for i in range(5)])
    grover = Grover(oracle, superposition_circuit=superposition)
    assert grover.oracle == oracle
    assert grover.superposition == superposition
    assert grover.sup_qubits == 5
    assert grover.sup_size == 32
    assert not grover.iterative
    grover = Grover(oracle,
                    superposition_circuit=superposition,
                    superposition_size=int(2**5))
    assert grover.oracle == oracle
    assert grover.superposition == superposition
    assert grover.sup_qubits == 5
    assert grover.sup_size == 32
    assert not grover.iterative
예제 #6
0
def test_grover_initial_state(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    oracle = Circuit(5 + 1)
    oracle.add(gates.X(5).controlled_by(*range(5)))
    initial_state = Circuit(5)
    initial_state.add(gates.X(4))
    grover = Grover(oracle, superposition_qubits=5, initial_state_circuit=initial_state, number_solutions=1)
    assert grover.initial_state_circuit == initial_state
    solution, iterations = grover(logs=True)
    assert solution == ["11111"]
    qibo.set_backend(original_backend)
예제 #7
0
def test_grover_wrong_solution(backend):
    def check(result):
        for i in result:
            if int(i) != 1:
                return False
        return True

    oracle = Circuit(5 + 1)
    oracle.add(gates.X(5).controlled_by(*range(5)))
    grover = Grover(oracle,
                    superposition_qubits=5,
                    check=check,
                    number_solutions=2)
    solution, iterations = grover(logs=True)
    assert len(solution) == 2
예제 #8
0
def test_grover_execute(backend, num_sol):
    def check(result):
        for i in result:
            if int(i) != 1:
                return False
        return True

    oracle = Circuit(5 + 1)
    oracle.add(gates.X(5).controlled_by(*range(5)))
    grover = Grover(oracle,
                    superposition_qubits=5,
                    check=check,
                    number_solutions=num_sol)
    solution, iterations = grover(freq=True, logs=True)
    if num_sol:
        assert solution == ["11111"]
        assert iterations == 4
    else:
        assert solution == "11111"