Einführung in Qiskit¶
When using Qiskit a user workflow nominally consists of following four high-level steps:
Build: Design a quantum circuit(s) that represents the problem you are considering.
** Compile**: Kompiliert Schaltungen für einen bestimmten Quantendienst, z.B. ein Quantensystem oder ein klassischer Simulator.
Run: Führt die kompilierten Schaltkreise auf dem/den angegebenen Quantendienst(en) aus. Diese Dienste können Cloud-basiert oder lokal sein.
Analyze: Berechnet zusammenfassende Statistiken und visualisiert die Ergebnisse der Experimente.
Hier ist ein Beispiel für den gesamten Workflow, wobei jeder Schritt in folgenden Abschnitten ausführlich erläutert wird:
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(compiled_circuit)
print("\nTotal count for 00 and 11 are:",counts)
# Draw the circuit
circuit.draw()
Total count for 00 and 11 are: {'11': 492, '00': 508}
┌───┐ ┌─┐ q_0: ┤ H ├──■──┤M├─── └───┘┌─┴─┐└╥┘┌─┐ q_1: ─────┤ X ├─╫─┤M├ └───┘ ║ └╥┘ c: 2/═══════════╩══╩═ 0 1
# Plot a histogram
plot_histogram(counts)

Workflow Schritt für Schritt¶
Das obige Programm kann in sechs Schritte untergliedert werden:
Pakete importieren
Variablen initialisieren
Add gates
Visualize the circuit
Simulate the experiment
Visualize the results
Schritt 1 : Pakete importieren¶
So werden die Grundbausteine für das Programm importiert:
import numpy as np
from qiskit import QuantumCircuit
from qiskit.providers.aer import QasmSimulator
from qiskit.visualization import plot_histogram
Im Einzelnen sind die Importe
Schritt 2: Variablen initialisieren¶
Betrachten Sie die nächste Codezeile
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¶
Sie können Gates (Operationen) hinzufügen, um die Register Ihrer Schaltung zu manipulieren.
Betrachten Sie die folgenden drei Zeilen 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
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)
: Ein Controlled-Not Operator (\(CNOT\)) mit Kontroll-Qubit 0 und Ziel-Qubit 1. Damit werden diese Qubits in einen quantenmechanisch verschränkten Zustand gebracht.QuantumCircuit.measure([0,1], [0,1])
: Werden die Quanten- und klassischen Register als Ganzes anmeasure
übergeben, wird das Messergebnis des i-ten Qubits im i-ten klassischen Bit gespeichert.
Step 4 : Visualize the Circuit¶
Sie können :meth:` qiskit.circuit.QuantumCircuit.draw ` verwenden, um die Schaltung die Sie entworfen haben so anzuzeigen, wie sie in vielen Lehrbüchern und Forschungsartikeln dargestellt werden.
circuit.draw()
┌───┐ ┌─┐ q_0: ┤ H ├──■──┤M├─── └───┘┌─┴─┐└╥┘┌─┐ q_1: ─────┤ X ├─╫─┤M├ └───┘ ║ └╥┘ c: 2/═══════════╩══╩═ 0 1
In this circuit, the qubits are ordered with qubit zero at the top and qubit one at the bottom. The circuit is read left-to-right, meaning that gates which are applied earlier in the circuit show up farther to the left.
The default backend for QuantumCircuit.draw()
or qiskit.visualization.circuit_drawer()
is the text backend. However, depending on your local environment you may want to change
these defaults to something better suited for your use case. This is done with the user
config file. By default the user config file should be located in
~/.qiskit/settings.conf
and is a .ini
file.
For example, a settings.conf
file for setting a Matplotlib drawer is:
[default]
circuit_drawer = mpl
You can use any of the valid circuit drawer backends as the value for this config, this includes text, mpl, latex, and latex_source.
Step 5 : Simulate the Experiment¶
Qiskit Aer ist ein Hochleistungs-Simulator-Framework für Quantenschaltungen. Es bietet ` mehrere Backends <apidoc/aer_provider.html#simulator-backends>` __, um verschiedene Simulationsziele zu erreichen.
Wenn Sie Probleme mit der Installation von Aer haben, können Sie alternativ den Basic-Aer-Provider verwenden, indem Sie ` Aer ` durch ` BasicAer ` ersetzen. Basic-Aer ist in Qiskit-Terra enthalten.
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': 513, '00': 487}
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 stellt viele Visualisierungen zur Verfügung,
inklusive der Funktion plot_histogram
um die Ergebnisse anzuzeigen.
plot_histogram(counts)

Die beobachteten Wahrscheinlichkeiten \(Pr(00)\) and \(Pr(11)\) ergeben sich aus den jeweiligen Auszählungen geteilt durch die Anzahl der Durchläufe.
Bemerkung
Versuchen Sie, das Schlüsselwort shots
in der Methode run()
zu ändern, um zu sehen, wie sich die geschätzten Wahrscheinlichkeiten ändern.
Nächste Schritte¶
Now that you have learnt the basics, consider these learning resources:
:ref:` Qiskit-Tutorials<tutorials>`
`Lehrbuch: Quantumt-Computing mit Qiskit lernen<https://community.qiskit.org/textbook/preface>`__