Пример #1
0
    def __init__(self,
                 protocol='Bell_teleport',
                 device='qasm_simulator',
                 live=False,
                 qasm_sim=False,
                 noise_model=None,
                 shots=1024,
                 save_results=False,
                 directory=None):  # QIP_Task initialisation function

        # Stores protocol information
        self.protocol = protocol
        self.live = live
        self.shots = shots
        self.save_results = save_results
        self.directory = directory

        if device == 'qasm_simulator':  # Defines settings for qasm_simulator use only
            self.qasm_sim = False
            self.backend = Aer.get_backend('qasm_simulator')
            self.device = self.backend
            self.coupling_map = None
            if noise_model:
                self.noise_model = noise_model
                self.basis_gates = self.noise_model.basis_gates
            else:
                self.noise_model = None
                self.basis_gates = None
        else:  # Defines settings for processor use
            self.qasm_sim = qasm_sim
            provider = IBMQ.get_provider(group='open')
            self.device = provider.get_backend(device)
            if save_results:
                plot_error_map(self.device).savefig('{}/error_map.svg'.format(
                    self.directory),
                                                    format='svg')
            if self.live:  # Defines settings for live simulations
                self.backend = self.device
                self.coupling_map = None
                self.noise_model = None
                self.basis_gates = None
            else:  # Defines settings for artificial simulations
                from qiskit.providers.aer.noise import NoiseModel

                self.backend = Aer.get_backend('qasm_simulator')
                self.properties = self.device.properties()
                self.coupling_map = self.device.configuration().coupling_map
                self.noise_model = NoiseModel.from_backend(self.properties)
                self.basis_gates = self.noise_model.basis_gates
def get_gate_info(backend):
    # Pull out the gates information.
    gates=backend.properties().gates
    
    #Cycle through the CX gate couplings to find the best and worst 
    cx_best_worst = [[[0,0],1],[[0,0],0]]
    for n in range (0, len(gates)):
        if gates[n].gate ==  "cx":
            print(gates[n].name, ":", gates[n].parameters[0].name,"=", gates[n].parameters[0].value)
            if cx_best_worst[0][1]>gates[n].parameters[0].value:
                cx_best_worst[0][1]=gates[n].parameters[0].value
                cx_best_worst[0][0]=gates[n].qubits
            if cx_best_worst[1][1]<gates[n].parameters[0].value:
                cx_best_worst[1][1]=gates[n].parameters[0].value
                cx_best_worst[1][0]=gates[n].qubits
    print("Best cx gate:", cx_best_worst[0][0], ",", round(cx_best_worst[0][1]*100,3),"%")
    print("Worst cx gate:", cx_best_worst[1][0], ",", round(cx_best_worst[1][1]*100,3),"%")
    display(plot_error_map(backend, show_title=True))
    return(cx_best_worst)
Пример #3
0
for n in range(0, len(available_backends)):
    backend = provider.get_backend(str(available_backends[n]))
    print("{0:20} {1:<10}".format(backend.name(),
                                  backend.configuration().n_qubits))

# Select a backend or go for the least busy backend with more than 1 qubits
backend_input = input("Enter the name of a backend, or X for the least busy:")
if backend_input not in ["X", "x"]:
    backend = provider.get_backend(backend_input)
else:
    backend = least_busy(
        provider.backends(filters=lambda b: b.configuration().n_qubits > 1 and
                          b.status().operational))
# Display the gate and error map for the backend.
print("\nQubit data for backend:", backend.status().backend_name)

display(plot_gate_map(backend, plot_directed=True))
display(plot_error_map(backend))

# Create and transpile a 2 qubit Bell circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)

display(qc.draw('mpl'))
qc_transpiled = transpile(qc, backend=backend, optimization_level=3)
display(qc_transpiled.draw('mpl'))

# Display the circuit layout for the backend.
display(plot_circuit_layout(qc_transpiled, backend, view='physical'))
Пример #4
0
    def collisional_model(self,
                          backend='qasm_simulator',
                          live=False,
                          noise_model=None,
                          channel='amplitude_damping',
                          shots=1024,
                          measured_qubits=(),
                          max_collisions=5,
                          theta=np.pi / 2,
                          concurrence=False,
                          tangle=False,
                          witness=False,
                          tangle_witness=True,
                          markov=True,
                          print_results=True,
                          save_results=False,
                          directory=None,
                          full=True,
                          initial_statevector=np.array([0.5, 0.5])):

        self.channel = channel
        self.markov = markov

        if self.state == 'h':
            self.qc.h(0)

        elif self.state == 'GHZ_teleport':
            initial_state = initial_statevector
            initial_state /= np.linalg.norm(initial_state)
            self.qc.initialize(initial_state, [0])
            self.theoretical_rho = np.outer(initial_state, initial_state)
            self.GHZ([1, 2, 3])

        elif self.state == 'D':
            self.D()
            if full:
                self.theoretical_rho = self.full_theoretical_D(self.n, self.k)
            else:
                self.theoretical_rho = self.theoretical_D(self.n, self.k)

        elif self.state == 'W':
            self.W()
            if full:
                self.theoretical_rho = self.full_theoretical_D(self.n, self.k)
            else:
                self.theoretical_rho = self.theoretical_D(self.n, self.k)

        elif self.state == 'GHZ':
            self.GHZ(range(self.n))
            if full:
                self.theoretical_rho = self.full_theoretical_GHZ(self.n)
            else:
                self.theoretical_rho = self.theoretical_GHZ(self.n)

        self.histogram_data = []
        self.directory = directory

        if backend == 'qasm_simulator':
            self.backend = Aer.get_backend('qasm_simulator')
            self.coupling_map = None
            if noise_model:
                self.noise_model = noise_model
                self.basis_gates = self.noise_model.basis_gates
            else:
                self.noise_model = None
                self.basis_gates = None
        else:
            provider = IBMQ.get_provider(group='open')
            self.device = provider.get_backend(backend)
            self.properties = self.device.properties()
            self.coupling_map = self.device.configuration().coupling_map
            self.noise_model = noise.device.basic_device_noise_model(
                self.properties)
            self.basis_gates = self.noise_model.basis_gates

            if save_results:
                visual.plot_error_map(self.device).savefig(
                    '{}/error_map.png'.format(self.directory))

            if live:
                check = input(
                    "Are you sure you want to run the circuit live? [y/n]: ")
                if check == 'y' or check == 'yes':
                    self.backend = self.device
                else:
                    self.backend = Aer.get_backend('qasm_simulator')
            else:
                self.backend = Aer.get_backend('qasm_simulator')

        self.unitary_backend = Aer.get_backend('unitary_simulator')

        self.shots = shots

        if self.state == 'GHZ_teleport':
            self.a = 3
            self.b = 3
        elif measured_qubits == ():
            self.a = 0
            self.b = self.n - 1
        else:
            self.a = measured_qubits[0]
            self.b = measured_qubits[1]

        self.max_collisions = max_collisions
        self.theta = theta

        if self.max_collisions > 0:
            if self.channel == 'phase_damping_one_qubit':
                self.ancilla = QuantumRegister(1, 'a')
                self.qc.add_register(self.ancilla)
            elif not markov:
                self.ancilla = QuantumRegister(3, 'a')
                self.qc.add_register(self.ancilla)
                self.qc.h(self.ancilla[2])
                self.qc.cx(self.ancilla[2], self.ancilla[1])
                self.qc.cx(self.ancilla[1], self.ancilla[0])
            else:
                self.ancilla = QuantumRegister(self.max_collisions, 'a')
                self.qc.add_register(self.ancilla)

        if self.channel == 'amplitude_damping':
            self.U = self.amplitude_damping_operator(full)
        elif self.channel == 'phase_damping' or 'phase_damping_one_qubit':
            self.U = self.phase_damping_operator(full)
        elif self.channel == 'heisenberg':
            print('Not yet programmed')
            quit()
            self.U = self.heisenberg_operator()

        if tangle_witness:
            self.tangle_witness_GHZ = 3 / 4 * np.kron(np.kron(
                I, I), I) - self.full_theoretical_GHZ(3)
            self.tangle_witness_tri = 1 / 2 * np.kron(np.kron(
                I, I), I) - self.full_theoretical_GHZ(3)

        counts_list = []
        rho_list = []
        theoretical_rho_list = []
        concurrence_list = []
        theoretical_concurrence_list = []
        tangle_ub_list = []
        tangle_lb_list = []
        theoretical_tangle_list = []
        fidelity_list = []
        witness_list = []
        tangle_witness_GHZ_list = []
        tangle_witness_tri_list = []
        trace_squared_list = []
        for k in range(self.max_collisions + 1):
            if k > 0:
                self.collision(k)
                if witness:
                    counts = self.measure(witness)
                else:
                    rho = self.tomography(full)
                    self.evolve_theoretical_rho(full)
            else:
                if witness:
                    counts = 1 / 2
                else:
                    rho = self.tomography(full)

            if save_results:
                visual.plot_state_city(rho).savefig(
                    '{}/state_city_{}.png'.format(self.directory, k))
                visual.plot_state_paulivec(rho).savefig(
                    '{}/paulivec_{}.png'.format(self.directory, k))

            if witness:
                counts_list.append(counts)
            else:
                rho_list.append(rho)
                theoretical_rho_list.append(self.theoretical_rho)
                if not full and self.state != 'GHZ_teleport':
                    C = self.concurrence(rho)
                    concurrence_list.append(C)
                    TC = self.concurrence(self.theoretical_rho)
                    theoretical_concurrence_list.append(TC)
                if tangle:
                    T_ub = self.tangle_ub(rho)
                    tangle_ub_list.append(T_ub)
                    #T_lb = self.tangle_lb(rho)
                    #tangle_lb_list.append(T_lb)
                    if self.state == 'GHZ':
                        TT = np.exp(np.log(np.cos(self.theta)) * k)
                        theoretical_tangle_list.append(TT)
                F = state_fidelity(theoretical_rho_list[0], rho)
                fidelity_list.append(F)
                if witness:
                    W = np.real(
                        np.trace(
                            np.matmul(self.full_theoretical_GHZ(self.n), rho)))
                    witness_list.append(W)
                if tangle_witness and full:
                    TWGHZ = np.real(
                        np.trace(np.matmul(self.tangle_witness_GHZ, rho)))
                    tangle_witness_GHZ_list.append(TWGHZ)
                    TWtri = np.real(
                        np.trace(np.matmul(self.tangle_witness_tri, rho)))
                    tangle_witness_tri_list.append(TWtri)
                Tr2 = np.real(np.trace(np.matmul(rho, rho)))
                trace_squared_list.append(Tr2)

                if print_results:
                    print("Collision Number:", k)
                    print("Original Density Matrix:")
                    print(theoretical_rho_list[0])
                    print("Theoretical Density Matrix:")
                    print(self.theoretical_rho)
                    print("Measured Density Matrix:")
                    print(rho)
                    print("Trace:", np.real(np.trace(rho)))
                    print("Trace Squared:", Tr2)
                    if concurrence:
                        print("Concurrence:", C)
                        print("Theoretical Concurrence:", TC)
                    if tangle:
                        print("Tangle Upper Bound:", T_ub)
                        #print("Tangle Lower Bound:", T_lb)
                        print("Theoretical Tangle:", TT)
                    print("Fidelity:", F)
                    if witness:
                        print("Witness:", W)
                    if tangle_witness and full:
                        print("GHZ Tangle Witness:", TWGHZ)
                        print("Tripartite Tangle Witness:", TWtri)
                    print("Eigenvalues:",
                          np.sort(np.real(np.linalg.eigvals(rho)))[::-1])
                    print(
                        "\n-----------------------------------------------------------------------------------\n"
                    )

        if print_results:
            if save_results:
                visual.circuit_drawer(
                    self.qc,
                    filename='{}/constructed_circuit.png'.format(
                        self.directory),
                    output='mpl')
            else:
                print(self.qc)
            print("Constructed Circuit Depth: ", self.qc.depth())
            try:
                transpiled_qc = transpile(self.qc,
                                          self.device,
                                          optimization_level=3)
                if save_results:
                    visual.circuit_drawer(
                        transpiled_qc,
                        filename='{}/transpiled_circuit.png'.format(
                            self.directory),
                        output='mpl')
                print("Transpiled Circuit Depth: ", self.qc.depth())
            except:
                pass
            print()

        if save_results:
            visual.plot_histogram(
                self.histogram_data, title=self.state).savefig(
                    '{}/histogram.png'.format(self.directory))

        return counts_list, theoretical_rho_list, rho_list, theoretical_concurrence_list, concurrence_list, \
               theoretical_tangle_list, tangle_ub_list, tangle_lb_list, fidelity_list, witness_list, \
               tangle_witness_GHZ_list, tangle_witness_tri_list, trace_squared_list
Пример #5
0
        "                                                                                      ",
        link[10], "              ", link[9], "\n"
        "                                                                                       |                |\n"
        "                                                                                  ",
        value_q[9], " -- ", link[8], " -- ", value_q[8], " -- ", link[7],
        " -- ", value_q[7])

    return 0


calculus_differencial(value_q)

print("Value qubits : ", value_q)

if backend_sim == quantum_computer:
    plot_error_map(quantum_computer)
    qubit_decoherence = nb_qubits
    value_decoherence = [0, 0, 0, 0, 0]
    for i in range(0, nb_qubits):
        if i % 2 == 0:
            value_decoherence[qubit_decoherence - 10] = (
                (value_q[qubit_decoherence] - value_q[i]) +
                (value_q[qubit_decoherence] - value_q[i + 1])) / 512
            print("q", i, "[", value_q[qubit_decoherence], "-", value_q[i],
                  "] + q", i + 1, "[", value_q[qubit_decoherence], "-",
                  value_q[i + 1], "] => ",
                  (value_q[qubit_decoherence] - value_q[i]) +
                  (value_q[qubit_decoherence] - value_q[i + 1]), "/ 512 = ",
                  value_decoherence[qubit_decoherence - 10])
            qubit_decoherence += 1