Beispiel #1
0
                                    qubits,
                                    unitary,
                                    extra_empty_bits=extra_empty_bits,
                                    custom_prepare=custom_prepare)

    final_qasm = optimize(final_qasm, nancillas, qubits, extra_empty_bits)

    if topology is not None:
        final_qasm = map_to_topology(topology, final_qasm)

    final_qasm = optimize(final_qasm, nancillas, qubits, extra_empty_bits)

    if error_toggle:
        final_qasm = introduce_error(final_qasm, mu, sigma)

    final_qasm = optimize(final_qasm, nancillas, qubits, extra_empty_bits)

    backend_type = qi.get_backend_type_by_name('QX single-node simulator')
    result = qi.execute_qasm(final_qasm,
                             backend_type=backend_type,
                             number_of_shots=shots)

    # Classical postprocessing
    fraction, error = print_result(
        remove_degeneracy(result['histogram'], nancillas),
        desired_bit_accuracy, nancillas)

    print('Fraction: ', fraction)
    print('Error: ', error)
    print('Correct chance: ', 1 - (1 - p_succes)**shots)
def testErrorCorrection():
    server_url = r'https://api.quantum-inspire.com'
    print('Enter mail address')
    email = input()

    print('Enter password')
    password = getpass()
    auth = (email, password)

    qi = QuantumInspireAPI(server_url, auth)
    backend = qi.get_backend_type_by_name('QX single-node simulator')

    inputValues = [
        {
            'theta': 0,
            'phi': 0
        },  #|0>
        {
            'theta': np.pi,
            'phi': np.pi
        },  #|1>
        {
            'theta': np.pi / 2,
            'phi': np.pi / 2
        }  #|+>
    ]

    for inputValue in inputValues:
        print("Using input value " + str(inputValue))
        allErrorIdxs = []
        for nrOfErrors in range(2):
            allErrorIdxs += itertools.combinations(range(nbqubits), nrOfErrors)
        # iterate over all possible combinations of errors

        for errorIdxs in allErrorIdxs:
            inputStateComplex = _stateForInput(inputValue['theta'],
                                               inputValue['phi'])

            print("Introducing errors, indices " + str(errorIdxs))
            QASM = createProgram(inputValue, errorIdxs)

            prob_0 = 0
            prob_1 = 0

            if 1:  # Wavefunction
                result = qi.execute_qasm(QASM,
                                         backend_type=backend,
                                         number_of_shots=1)
            else:  # Do measurements
                result = qi.execute_qasm(QASM,
                                         backend_type=backend,
                                         number_of_shots=10)

            result = result['histogram']
            for key, value in zip(result.keys(), result.values()):
                if (int(key) % 2) == 0:  #Number is odd
                    prob_0 += value
                else:
                    prob_1 += value

            if abs(prob_0 - abs(inputStateComplex[0])**2) < 0.0001 and abs(
                    prob_1 - abs(inputStateComplex[1])**2) < 0.0001:
                print("Error correction works")
            else:
                print("Error correction fails")