Esempio n. 1
0
def test_collapse_gate(backend, nqubits, targets, results, oncircuit):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)

    initial_state = utils.random_numpy_state(nqubits)
    if oncircuit:
        c = Circuit(nqubits)
        c.add(gates.Collapse(*targets, result=results))
        final_state = c(np.copy(initial_state)).numpy()
    else:
        collapse = gates.Collapse(*targets, result=results)
        if backend == "custom":
            final_state = collapse(np.copy(initial_state)).numpy()
        else:
            original_shape = initial_state.shape
            new_shape = nqubits * (2, )
            final_state = collapse(np.copy(initial_state).reshape(new_shape))
            final_state = final_state.numpy().reshape(original_shape)

    if isinstance(results, int):
        results = nqubits * [results]
    slicer = nqubits * [slice(None)]
    for t, r in zip(targets, results):
        slicer[t] = r
    slicer = tuple(slicer)
    initial_state = initial_state.reshape(nqubits * (2, ))
    target_state = np.zeros_like(initial_state)
    target_state[slicer] = initial_state[slicer]
    norm = (np.abs(target_state)**2).sum()
    target_state = target_state.ravel() / np.sqrt(norm)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
Esempio n. 2
0
def test_thermal_relaxation_channel_repeated(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)

    initial_state = random_state(5)
    c = Circuit(5)
    c.add(gates.ThermalRelaxationChannel(4, t1=1.0, t2=0.6, time=0.8,
                                         excited_population=0.8, seed=123))
    final_state = c(np.copy(initial_state), nshots=30)

    pz, p0, p1 = c.queue[0].calculate_probabilities(1.0, 0.6, 0.8, 0.8)
    np.random.seed(123)
    target_state = []
    for _ in range(30):
        noiseless_c = Circuit(5)
        if np.random.random() < pz:
            noiseless_c.add(gates.Z(4))
        if np.random.random() < p0:
            noiseless_c.add(gates.Collapse(4))
        if np.random.random() < p1:
            noiseless_c.add(gates.Collapse(4))
            noiseless_c.add(gates.X(4))
        target_state.append(noiseless_c(np.copy(initial_state)))
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
Esempio n. 3
0
def test_collapse_gate_errors():
    # pass wrong result length
    with pytest.raises(ValueError):
        gate = gates.Collapse(0, 1, result=[0, 1, 0])
    # pass wrong result values
    with pytest.raises(ValueError):
        gate = gates.Collapse(0, 1, result=[0, 2])
    # change result after creation
    gate = gates.Collapse(2, 0, result=[0, 0])
    gate.nqubits = 4
    gate.result = np.ones(2, dtype=np.int)
Esempio n. 4
0
def test_collapse_gate(backend, nqubits, targets, results):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    initial_psi = utils.random_numpy_state(nqubits)
    initial_rho = np.outer(initial_psi, initial_psi.conj())
    c = models.Circuit(nqubits, density_matrix=True)
    c.add(gates.Collapse(*targets, result=results))
    final_rho = c(np.copy(initial_rho))

    c = models.Circuit(nqubits)
    c.add(gates.Collapse(*targets, result=results))
    target_psi = c(np.copy(initial_psi)).numpy()
    target_rho = np.outer(target_psi, target_psi.conj())
    np.testing.assert_allclose(final_rho, target_rho)
    qibo.set_backend(original_backend)
Esempio n. 5
0
def quantum_order_finding_semiclassical(N, a):
    """Quantum circuit that performs the order finding algorithm using a semiclassical iQFT.
    Args:
        N (int): number to factorize.
        a (int): chosen number to use in the algorithm.

    Returns:
        s (float): value of the state measured by the quantum computer.
    """
    print('  - Performing algorithm using a semiclassical iQFT.\n')
    # Creating the parts of the needed quantum circuit.
    n = int(np.ceil(np.log2(N)))
    b = [i for i in range(n+1)]
    x = [n+1+i for i in range(n)]
    ancilla = 2*n + 1
    q_reg = 2*n + 2
    circuit = Circuit(2*n+3)
    print(f'  - Total number of qubits used: {2*n+3}.\n')
    r = []
    exponents = []
    exp = a%N
    for i in range(2*n):
        exponents.append(exp)
        exp = (exp**2)%N
    
    # Building the quantum circuit
    circuit.add(gates.H(q_reg))
    circuit.add(gates.X(x[len(x)-1]))
    #a_i = (a**(2**(2*n - 1)))
    circuit.add(c_U(q_reg, x, b, exponents[-1], N, ancilla, n))
    circuit.add(gates.H(q_reg))
    circuit.add(gates.M(q_reg))
    result = circuit(nshots=1)
    r.append(result.frequencies(binary=False).most_common()[0][0])
    initial_state = circuit.final_state
    
    # Using multiple measurements for the semiclassical QFT.
    for i in range(1, 2*n):
        circuit = Circuit(2*n+3)
        circuit.add(gates.Collapse(q_reg, result=[r[-1]]))
        if r[-1] == 1:
            circuit.add(gates.X(q_reg))
        circuit.add(gates.H(q_reg))
        #a_i = (a**(2**(2*n - 1 - i)))
        circuit.add(c_U(q_reg, x, b, exponents[-1-i], N, ancilla, n))
        angle = 0
        for k in range(2, i+2):
            angle += 2*np.pi*r[i+1-k]/(2**k)
        circuit.add(gates.U1(q_reg, -angle))
        circuit.add(gates.H(q_reg))
        circuit.add(gates.M(q_reg))
        result = circuit(initial_state, nshots=1)
        r.append(result.frequencies(binary=False).most_common()[0][0])
        initial_state = circuit.final_state
    s = 0
    for i in range(2*n):
        s += r[i]*2**(i)
    print(f"The quantum circuit measures s = {s}.\n")
    return s
Esempio n. 6
0
def test_collapse_gate_errors(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    # pass wrong result length
    with pytest.raises(ValueError):
        gate = gates.Collapse(0, 1, result=[0, 1, 0])
    # pass wrong result values
    with pytest.raises(ValueError):
        gate = gates.Collapse(0, 1, result=[0, 2])
    # attempt to construct unitary
    gate = gates.Collapse(2, 0, result=[0, 0])
    with pytest.raises(ValueError):
        gate.construct_unitary()
    # change result after creation
    gate = gates.Collapse(2, 0, result=[0, 0])
    gate.nqubits = 4
    gate.prepare()
    gate.result = np.ones(2, dtype=int)
    qibo.set_backend(original_backend)
Esempio n. 7
0
def test_reset_channel_repeated(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)

    initial_state = random_state(5)
    c = Circuit(5)
    c.add(gates.ResetChannel(2, p0=0.3, p1=0.3, seed=123))
    final_state = c(np.copy(initial_state), nshots=30)

    np.random.seed(123)
    target_state = []
    for _ in range(30):
        noiseless_c = Circuit(5)
        if np.random.random() < 0.3:
            noiseless_c.add(gates.Collapse(2))
        if np.random.random() < 0.3:
            noiseless_c.add(gates.Collapse(2))
            noiseless_c.add(gates.X(2))
        target_state.append(noiseless_c(np.copy(initial_state)))
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
Esempio n. 8
0
def test_collapse_gate_distributed(accelerators, nqubits, targets):
    initial_state = utils.random_numpy_state(nqubits)
    c = Circuit(nqubits, accelerators)
    c.add(gates.Collapse(*targets))
    final_state = c(np.copy(initial_state)).numpy()

    slicer = nqubits * [slice(None)]
    for t in targets:
        slicer[t] = 0
    slicer = tuple(slicer)
    initial_state = initial_state.reshape(nqubits * (2, ))
    target_state = np.zeros_like(initial_state)
    target_state[slicer] = initial_state[slicer]
    norm = (np.abs(target_state)**2).sum()
    target_state = target_state.ravel() / np.sqrt(norm)
    np.testing.assert_allclose(final_state, target_state)
Esempio n. 9
0
def test_collapse_gate_distributed(backend, accelerators, nqubits, targets):
    """Check :class:`qibo.core.cgates.Collapse` as part of distributed circuits."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    initial_state = random_state(nqubits)
    c = Circuit(nqubits, accelerators)
    c.add(gates.Collapse(*targets))
    final_state = c(np.copy(initial_state))
    slicer = nqubits * [slice(None)]
    for t in targets:
        slicer[t] = 0
    slicer = tuple(slicer)
    initial_state = initial_state.reshape(nqubits * (2,))
    target_state = np.zeros_like(initial_state)
    target_state[slicer] = initial_state[slicer]
    norm = (np.abs(target_state) ** 2).sum()
    target_state = target_state.ravel() / np.sqrt(norm)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
Esempio n. 10
0
def test_collapse_gate(backend, nqubits, targets, results):
    from qibo import K
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    initial_state = random_state(nqubits)
    collapse = gates.Collapse(*targets, result=results)
    final_state = apply_gates([collapse], initial_state=np.copy(initial_state))
    if isinstance(results, int) or isinstance(results, K.numeric_types):
        results = nqubits * [results]
    slicer = nqubits * [slice(None)]
    for t, r in zip(targets, results):
        slicer[t] = r
    slicer = tuple(slicer)
    initial_state = initial_state.reshape(nqubits * (2, ))
    target_state = np.zeros_like(initial_state)
    target_state[slicer] = initial_state[slicer]
    norm = (np.abs(target_state)**2).sum()
    target_state = target_state.ravel() / np.sqrt(norm)
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
Esempio n. 11
0
def test_collapse_after_measurement(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    qubits = [0, 2, 3]

    c1 = Circuit(5)
    c1.add((gates.H(i) for i in range(5)))
    c1.add(gates.M(*qubits))
    result = c1(nshots=1)
    c2 = Circuit(5)
    bitstring = result.samples(binary=True)[0]
    c2.add(gates.Collapse(*qubits, result=bitstring))
    c2.add((gates.H(i) for i in range(5)))
    final_state = c2(initial_state=c1.final_state)

    ct = Circuit(5)
    for i, r in zip(qubits, bitstring):
        if r:
            ct.add(gates.X(i))
    ct.add((gates.H(i) for i in qubits))
    target_state = ct()
    np.testing.assert_allclose(final_state, target_state, atol=1e-15)
    qibo.set_backend(original_backend)
def test_collapse_gate(backend, nqubits, targets, results):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    initial_rho = random_density_matrix(nqubits)
    gate = gates.Collapse(*targets, result=results)
    gate.density_matrix = True
    final_rho = gate(np.copy(initial_rho))

    target_rho = np.reshape(initial_rho, 2 * nqubits * (2, ))
    if isinstance(results, int):
        results = len(targets) * [results]
    for q, r in zip(targets, results):
        slicer = 2 * nqubits * [slice(None)]
        slicer[q], slicer[q + nqubits] = 1 - r, 1 - r
        target_rho[tuple(slicer)] = 0
        slicer[q], slicer[q + nqubits] = r, 1 - r
        target_rho[tuple(slicer)] = 0
        slicer[q], slicer[q + nqubits] = 1 - r, r
        target_rho[tuple(slicer)] = 0
    target_rho = np.reshape(target_rho, initial_rho.shape)
    target_rho = target_rho / np.trace(target_rho)
    np.testing.assert_allclose(final_rho, target_rho)
    qibo.set_backend(original_backend)