2020-10-13 16:34:24 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
2020-10-14 05:58:52 +00:00
|
|
|
Build a simple bare-minimum quantum circuit that starts with a single
|
|
|
|
qubit (by default, in state 0) and inverts it. Run the experiment 1000
|
2020-10-13 16:34:24 +00:00
|
|
|
times and print the total count of the states finally observed.
|
|
|
|
Qiskit Docs: https://qiskit.org/documentation/getting_started.html
|
|
|
|
"""
|
|
|
|
|
|
|
|
import qiskit as q
|
|
|
|
|
|
|
|
|
|
|
|
def single_qubit_measure(qubits: int, classical_bits: int) -> q.result.counts.Counts:
|
|
|
|
"""
|
2020-10-14 05:58:52 +00:00
|
|
|
>>> single_qubit_measure(2, 2)
|
2020-10-13 16:34:24 +00:00
|
|
|
{'11': 1000}
|
2020-10-14 05:58:52 +00:00
|
|
|
>>> single_qubit_measure(4, 4)
|
|
|
|
{'0011': 1000}
|
2020-10-13 16:34:24 +00:00
|
|
|
"""
|
|
|
|
# Use Aer's qasm_simulator
|
2020-10-14 05:58:52 +00:00
|
|
|
simulator = q.Aer.get_backend("qasm_simulator")
|
2020-10-13 16:34:24 +00:00
|
|
|
|
|
|
|
# Create a Quantum Circuit acting on the q register
|
|
|
|
circuit = q.QuantumCircuit(qubits, classical_bits)
|
|
|
|
|
|
|
|
# Apply X (NOT) Gate to Qubits 0 & 1
|
|
|
|
circuit.x(0)
|
|
|
|
circuit.x(1)
|
|
|
|
|
|
|
|
# Map the quantum measurement to the classical bits
|
|
|
|
circuit.measure([0, 1], [0, 1])
|
|
|
|
|
|
|
|
# Execute the circuit on the qasm simulator
|
|
|
|
job = q.execute(circuit, simulator, shots=1000)
|
|
|
|
|
|
|
|
# Return the histogram data of the results of the experiment.
|
|
|
|
return job.result().get_counts(circuit)
|
|
|
|
|
|
|
|
|
2020-10-14 05:58:52 +00:00
|
|
|
if __name__ == "__main__":
|
2020-10-13 16:34:24 +00:00
|
|
|
counts = single_qubit_measure(2, 2)
|
2020-10-14 05:58:52 +00:00
|
|
|
print(f"Total count for various states are: {counts}")
|