File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ """
3+ Build a simple bare-minimum quantum circuit that starts with a single
4+ qubit (by default, in state 0) and inverts it. Run the experiment 1000
5+ times and print the total count of the states finally observed.
6+ Qiskit Docs: https://qiskit.org/documentation/getting_started.html
7+ """
8+
9+ import qiskit as q
10+
11+
12+ def single_qubit_measure (qubits : int , classical_bits : int ) -> q .result .counts .Counts :
13+ """
14+ >>> single_qubit_measure(1, 1)
15+ {'11': 1000}
16+ """
17+ # Use Aer's qasm_simulator
18+ simulator = q .Aer .get_backend ('qasm_simulator' )
19+
20+ # Create a Quantum Circuit acting on the q register
21+ circuit = q .QuantumCircuit (qubits , classical_bits )
22+
23+ # Apply X (NOT) Gate to Qubits 0 & 1
24+ circuit .x (0 )
25+ circuit .x (1 )
26+
27+ # Map the quantum measurement to the classical bits
28+ circuit .measure ([0 , 1 ], [0 , 1 ])
29+
30+ # Execute the circuit on the qasm simulator
31+ job = q .execute (circuit , simulator , shots = 1000 )
32+
33+ # Return the histogram data of the results of the experiment.
34+ return job .result ().get_counts (circuit )
35+
36+
37+ if __name__ == '__main__' :
38+ counts = single_qubit_measure (2 , 2 )
39+ print (f'Total count for various states are: { counts } ' )
You can’t perform that action at this time.
0 commit comments