-
-
Save pedrominicz/65895fc99715f55f170346662e0126b0 to your computer and use it in GitHub Desktop.
Simple Qiskit example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from qiskit import * | |
from qiskit.circuit.library import * | |
sim = Aer.get_backend('qasm_simulator') | |
# Create a quantum circuit with 2 qubits and 2 classical bits. | |
qc = QuantumCircuit(2, 2) | |
# Add a single-qubit Hadamard gate on qubit 0. | |
qc.h(0) | |
# Add a controlled-X (controlled-Not) gate on control qubit 0 and target qubit | |
# 1. | |
qc.cx(0, 1) | |
# Measure qubits 0 and 1 to classical bits 0 and 1 respectively. | |
qc.measure([0, 1], [0, 1]) | |
# `shots` specified the number of repetitions for each circuit, for sampling. | |
# The default is `1024`. | |
job = execute(qc, sim, shots=1000) | |
result = job.result() | |
counts = result.get_counts(qc) | |
print(counts) | |
# The circuit created forms a Bell state. A Bell state is the simplest example | |
# of entanglement. The `H` gate puts `q_0` into a superposition then the `X` | |
# gate (controlled-Not) entangles both qubits. When measured, the qubits will | |
# collapse to either `0` or `1` and, by the particular way they are entangled, | |
# `q_1 = q_0 ^ q_1`. | |
# | |
# ┌───┐ ┌─┐ | |
# q_0: ┤ H ├──■──┤M├─── | |
# └───┘┌─┴─┐└╥┘┌─┐ | |
# q_1: ─────┤ X ├─╫─┤M├ | |
# └───┘ ║ └╥┘ | |
# c: 2/═══════════╩══╩═ | |
# 0 1 | |
# | |
print(qc.draw()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment