使用平台为Jupyter Notebook。

QISKit tutorial – Bell State

This project is based on the IBM QISKit tutorial on Github. The code can be found in bell state link and visualization link.

If this is the first time for you to run QISKit, please follow the following steps to install the environment:

  1. Install Anaconda 3 (Can be downloaded here)
  2. Create a new environment for running QISKit
conda create -n env_name python=3.6

Note that env_name is the name you given to this new environment. Here this line is to install the current latest version for python – version 3.6, and QISKit is only available with python 3.5 or later.
3. Activate the newly generated environment

conda activate env_name
  1. Install QISKit package
pip install qiskit

QISKit is available on these platforms: Linux x86_64, Darwin and Win64.
5. Install Jupyter Notebook

pip install jupyter
  1. Run Jupyter Notebook
jupyter notebook

Part 1: Use remote backend

Now we have finished all the installation. If you want to run QISKit locally, you can start the coding part now.

But if you want to execute the code on a quantum chip, there is one last step left, which is to get an IBM Q token:

  1. Create an IBM Q account if you do not have one.
  2. Go to My Account > Advanced > API Token to get your token.
  3. Copy your token and paste it below:
# If you want to execute your code on a quantum chip:
My_token = 'XXX' # Paste your token here
from qiskit import IBMQ
# Store API credentials locally
IBMQ.save_account(My_token)

Here after this process, a file named qiskit.ipynb will be created. You should find the location of that file on your computer, and create another file named Qconfig.py in that folder with the following content:

APItoken = 'PUT_YOUR_API_TOKEN_HERE'

config = {
    'url': 'https://quantumexperience.ng.bluemix.net/api'
}

A more detailed tutorial for this process can be found here.

# If you want to execute your code on a quantum chip:
from qiskit.backends.ibmq import least_busy
# Authenticate for access to remote backends

try:
    import Qconfig
    IBMQ.enable_account(Qconfig.APItoken, Qconfig.config['url'])
except:
    print("""WARNING: There's no connection with the API for remote backends.
             Have you initialized a Qconfig.py file with your personal token?
             For now, there's only access to local simulator backends...""")
# Import the Qiskit SDK
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute, IBMQ

# Create a Quantum Register with 2 qubits.
q = QuantumRegister(2)
print(q[0])
print(q[1])
# Create a Classical Register with 2 bits.
c = ClassicalRegister(2)
print(c[0])
print(c[1])
# Create a Quantum Circuit
qc = QuantumCircuit(q, c)
# Add a H gate on qubit 0, putting this qubit in superposition.
qc.h(q[0])
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting
# the qubits in a Bell state.
qc.cx(q[0], q[1])
# Add a Measure gate to see the state.
qc.measure(q, c)
(QuantumRegister(2, 'q0'), 0)
(QuantumRegister(2, 'q0'), 1)
(ClassicalRegister(2, 'c0'), 0)
(ClassicalRegister(2, 'c0'), 1)





<qiskit._instructionset.InstructionSet at 0x1ec8e024160>

In most cases, the remote devices we are able to use are:

  • ibmqx4: 5-qubit backend

  • ibmq_16_melbourne: 16-qubit backend

# see a list of available remote backends
ibmq_backends = IBMQ.backends()
print("Remote backends: ", ibmq_backends)
# Compile and run the Quantum Program on a real device backend
try:
    least_busy_device = least_busy(IBMQ.backends(simulator=False))
    print("Running on current least busy device: ", least_busy_device)
    # runing the job
    job_exp = execute(qc, least_busy_device, shots=1024, max_credits=10)
    #job_exp = execute(qc, backend_ibmq, shots=1024, max_credits=10)
    result_exp = job_exp.result()
    # Show the results
    print("experiment: ", result_exp)
    print(result_exp.get_counts(qc))
except:
    print("All devices are currently unavailable.")

The result is not 50-50 divided for cases ‘00’ and ‘11’, because it is what you would expect from running on a noisy quatum device.

The ideal result without any noise can be found in Part 2.

import matplotlib.pyplot as plt
dict_result = result_exp.get_counts(qc)
x = ['00', '01', '10', '11']
data = []
for i in range(len(x)):
    data.append(dict_result[x[i]])
plt.bar(x, data)
plt.show()

如何使用QISKit量子程序

Part 2: Use local simulator

For some cases, it may take some time to run on the busy remote backends, then it is better to use the local simulator.

# Import the Qiskit SDK
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute, Aer

# Create a Quantum Register with 2 qubits.
q = QuantumRegister(2)
print(q[0])
print(q[1])
# Create a Classical Register with 2 bits.
c = ClassicalRegister(2)
print(c[0])
print(c[1])
# Create a Quantum Circuit
qc = QuantumCircuit(q, c)
# Add a H gate on qubit 0, putting this qubit in superposition.
qc.h(q[0])
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting
# the qubits in a Bell state.
qc.cx(q[0], q[1])
# Add a Measure gate to see the state.
qc.measure(q, c)
(QuantumRegister(2, 'q1'), 0)
(QuantumRegister(2, 'q1'), 1)
(ClassicalRegister(2, 'c1'), 0)
(ClassicalRegister(2, 'c1'), 1)





<qiskit._instructionset.InstructionSet at 0x16c7dd7cc18>
# See a list of available local simulators
print("Aer backends: ", Aer.backends())
# Compile and run the Quantum circuit on a simulator backend
backend_sim = Aer.get_backend('qasm_simulator')
job_sim = execute(qc, backend_sim, shots=1024, max_credits=10)
result_sim = job_sim.result()
# Show the results
print("simulation: ", result_sim )
print(result_sim.get_counts(qc))
Aer backends:  [<QasmSimulator('qasm_simulator') from Aer()>, <QasmSimulatorPy('qasm_simulator_py') from Aer()>, <StatevectorSimulator('statevector_simulator') from Aer()>, <StatevectorSimulatorPy('statevector_simulator_py') from Aer()>, <UnitarySimulator('unitary_simulator') from Aer()>, <CliffordSimulator('clifford_simulator') from Aer()>]
simulation:  COMPLETED
{'00': 498, '11': 526}
import matplotlib.pyplot as plt
dict_result = result_sim.get_counts(qc)
# x = ['00', '11']
x = ['00', '01', '10', '11']
data = []
for i in range(len(x)):
    try:
        data.append(dict_result[x[i]])
    except:
        data.append(0)
plt.bar(x, data)
plt.show()

如何使用QISKit量子程序

Part 3: Visualization of Quantum Circuit

In this part, the code is the same for both part 1 and part 2

# Visualizing quantum circuit
from qiskit.tools.visualization import circuit_drawer

# Create the circuit
bell_circuit = qc

# Provide a name to write the diagram to the filesystem
circuit_drawer(bell_circuit, filename='./bell_circuit.png')

# Use the return value with show() to display the diagram
#diagram = circuit_drawer(bell_circuit)
#diagram.show()
WARNING: Unable to compile latex. Is `pdflatex` installed? Skipping latex circuit drawing...

如何使用QISKit量子程序

相关文章:

  • 2021-04-19
  • 2021-05-28
  • 2021-11-08
  • 2021-09-10
  • 2021-09-22
  • 2022-01-24
  • 2021-04-26
猜你喜欢
  • 2021-06-10
  • 2021-05-15
  • 2021-12-03
  • 2021-10-09
  • 2022-02-06
  • 2022-12-23
  • 2021-06-11
相关资源
相似解决方案