Qiskit Introduction

Posted 安徽思远

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qiskit Introduction相关的知识,希望对你有一定的参考价值。

Here is an example of the entire workflow, with each step explained in detail in subsequent sections:

import numpy as np
from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import QasmSimulator
from qiskit.visualization import plot_histogram

# Use Aer's qasm_simulator
simulator = QasmSimulator()

# Create a Quantum Circuit acting on the q register
circuit = QuantumCircuit(2, 2)

# Add a H gate on qubit 0
circuit.h(0)

# Add a CX (CNOT) gate on control qubit 0 and target qubit 1
circuit.cx(0, 1)

# Map the quantum measurement to the classical bits
circuit.measure([0,1], [0,1])

# compile the circuit down to low-level QASM instructions
# supported by the backend (not needed for simple circuits)
compiled_circuit = transpile(circuit, simulator)

# Execute the circuit on the qasm simulator
job = simulator.run(compiled_circuit, shots=1000)

# Grab results from the job
result = job.result()

# Returns counts
counts = result.get_counts(circuit)
print("\\nTotal count for 00 and 11 are:",counts)

# Draw the circuit
circuit.draw()

Total count for 00 and 11 are: {‘11’: 503, ‘00’: 497}

Plot a histogram

plot_histogram(counts)

The program above can be broken down into six steps:

  • Step 1: Import packages

  • Step 2: Initialize variables

  • Step 3: Add gates

  • Step 4: Visualize the circuit

  • Step 5: Simulate the experiment

  • Step 6: Visualize the results


Step 1 : Import Packages
The basic elements needed for your program are imported as follows:

import numpy as np
from qiskit import QuantumCircuit
from qiskit.providers.aer import QasmSimulator
from qiskit.visualization import plot_histogram

In more detail, the imports are

QuantumCircuit: can be thought as the instructions of the quantum system. It holds all your quantum operations.

QasmSimulator: is the Aer high performance circuit simulator.

plot_histogram: creates histograms.

Step 2 : Initialize Variables
Consider the next line of code

circuit = QuantumCircuit(2, 2)

Here, you are initializing with 2 qubits in the zero state; with 2 classical bits set to zero; and circuit is the quantum circuit.

Syntax:

QuantumCircuit(int, int)

Step 3 : Add Gates
You can add gates (operations) to manipulate the registers of your circuit.

Consider the following three lines of code:

circuit.h(0)
circuit.cx(0, 1)
circuit.measure([0,1], [0,1])

The gates are added to the circuit one-by-one to form the Bell state:
∣ ψ ⟩ = ∣ 00 ⟩ + ∣ 11 ⟩ 2 |ψ⟩ = \\frac{|00⟩+|11⟩}{\\sqrt{2}} ψ=2 00+11

The code above applies the following gates:

  • QuantumCircuit.h(0): A Hadamard gate H on qubit 0, which puts it into
    a superposition state.
  • QuantumCircuit.cx(0, 1): A controlled-Not operation (CNOT) on control
    qubit 0 and target qubit 1, putting the qubits in an entangled state.
  • QuantumCircuit.measure([0,1], [0,1]): if you pass the entire quantum and classical registers to measure, the ith qubit’s measurement result will be stored in the ith classical bit.

Step 4 : Visualize the Circuit
You can use qiskit.circuit.QuantumCircuit.draw() to view the circuit that you have designed in the various forms used in many textbooks and research articles.

circuit.draw()

在这里插入图片描述

在这里插入图片描述
Note: the measurement operations match the qubit number to the classic bit number; in this case, qubit 0 will read out to bit 0, and qubit 1 will read out to bit 1, where bit 0 is the least significant bit.

Step 5 : Simulate the Experiment
Qiskit Aer is a high performance simulator framework for quantum circuits. It provides several backends to achieve different simulation goals.

If you have issues installing Aer, you can alternatively use the Basic Aer provider by replacing Aer with BasicAer. Basic Aer is included in Qiskit Terra.

import numpy as np
from qiskit import QuantumCircuit, transpile
from qiskit.providers.basicaer import QasmSimulatorPy

To simulate this circuit, you will use the qasm_simulator. Each run of this circuit will yield either the bit string 00 or 11.

simulator = QasmSimulator()
compiled_circuit = transpile(circuit, simulator)
job = simulator.run(compiled_circuit, shots=1000)
result = job.result()
counts = result.get_counts(circuit)
print("\\nTotal count for 00 and 11 are:",counts)

Total count for 00 and 11 are: {‘11’: 481, ‘00’: 519}

As expected, the output bit string is 00 approximately 50 percent of the time. The number of times the circuit is run can be specified via the shots argument of the execute method. The number of shots of the simulation was set to be 1000 (the default is 1024).

Once you have a result object, you can access the counts via the method get_counts(circuit). This gives you the aggregate outcomes of the experiment you ran.

Step 6 : Visualize the Results
Qiskit provides many visualizations,

including the function plot_histogram, to view your results.

plot_histogram(counts)

The observed probabilities Pr(00) and Pr(11) are computed by taking the respective counts and dividing by the total number of shots.
在这里插入图片描述

以上是关于Qiskit Introduction的主要内容,如果未能解决你的问题,请参考以下文章

从源代码安装 qiskit-aer 时出现 CMake 错误

量子计算与量子信息之Python-qiskit实现量子隐形传态

量子计算与量子信息之Python-qiskit第一个量子电路

CSP Layout 是不是始终是 qiskit 转译器用于映射量子电路的第一个算法?

QISKIT 错误 - numpy.ndarray 大小已更改,可能表示二进制不兼容。预期来自 C 标头的 88,从 PyObject 获得 80

ICE introduction to programmin