Skip to content

Commit 98f1220

Browse files
committed
quantum computing article
1 parent eacafb3 commit 98f1220

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+5296
-40
lines changed

content/posts/physics/quantum_computing/example.ipynb

+84-17
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
```python
2+
from qiskit import QuantumCircuit
3+
from qiskit.quantum_info import SparsePauliOp
4+
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
5+
# from qiskit_ibm_runtime import EstimatorV2 as Estimator
6+
7+
# Create a new circuit with two qubits
8+
qc = QuantumCircuit(2)
9+
10+
# Add a Hadamard gate to qubit 0
11+
qc.h(0)
12+
13+
# Perform a controlled-X gate on qubit 1, controlled by qubit 0
14+
qc.cx(0, 1)
15+
16+
# Return a drawing of the circuit using MatPlotLib ("mpl"). This is the
17+
# last line of the cell, so the drawing appears in the cell output.
18+
# Remove the "mpl" argument to get a text drawing.
19+
qc.draw("mpl")
20+
```
21+
22+
23+
24+
25+
26+
![png](example_files/example_0_0.png)
27+
28+
29+
30+
31+
32+
```python
33+
# # Create a Quantum Circuit with 2 qubits and 2 classical bits
34+
# qc = QuantumCircuit(2, 2)
35+
36+
# # Apply Hadamard gates to both qubits
37+
# qc.h([0, 1])
38+
39+
# # Apply the oracle (X gate to qubit 1)
40+
# qc.x(1)
41+
42+
# # Apply a CNOT gate with qubit 0 as control and qubit 1 as target
43+
# qc.cx(0, 1)
44+
45+
# # Apply the oracle (X gate to qubit 1)
46+
# qc.x(1)
47+
48+
# # Apply Hadamard gates to both qubits
49+
# qc.h([0, 1])
50+
51+
# # Measure the qubits
52+
# qc.measure([0, 1], [0, 1])
53+
54+
# # Draw the circuit
55+
# qc.draw('mpl')
56+
```
57+
58+
59+
```python
60+
# Set up six different observables.
61+
from qiskit.quantum_info import SparsePauliOp
62+
63+
observables_labels = ["ZZ", "ZI", "IZ", "XX", "XI", "IX"]
64+
observables = [SparsePauliOp(label) for label in observables_labels]
65+
66+
observables
67+
```
68+
69+
70+
71+
72+
[SparsePauliOp(['ZZ'],
73+
coeffs=[1.+0.j]),
74+
SparsePauliOp(['ZI'],
75+
coeffs=[1.+0.j]),
76+
SparsePauliOp(['IZ'],
77+
coeffs=[1.+0.j]),
78+
SparsePauliOp(['XX'],
79+
coeffs=[1.+0.j]),
80+
SparsePauliOp(['XI'],
81+
coeffs=[1.+0.j]),
82+
SparsePauliOp(['IX'],
83+
coeffs=[1.+0.j])]
84+
85+
86+
87+
88+
```python
89+
from qiskit_aer.primitives import Estimator
90+
91+
estimator = Estimator()
92+
93+
job = estimator.run([qc]*len(observables), observables)
94+
job.result()
95+
```
96+
97+
98+
99+
100+
EstimatorResult(values=array([ 1. , -0.0390625 , -0.0390625 , 1. , -0.04296875,
101+
-0.04296875]), metadata=[{'shots': 1024, 'variance': 0.0, 'simulator_metadata': [{'batched_shots_optimization': False, 'required_memory_mb': 0, 'method': 'stabilizer', 'active_input_qubits': [0, 1], 'device': 'CPU', 'remapped_qubits': False, 'num_qubits': 2, 'num_clbits': 2, 'time_taken': 0.0034544, 'sample_measure_time': 0.0009696, 'input_qubit_map': [[0, 0], [1, 1]], 'max_memory_mb': 7563, 'measure_sampling': True, 'noise': 'ideal', 'parallel_shots': 1, 'parallel_state_update': 16, 'runtime_parameter_bind': False, 'num_bind_params': 1, 'fusion': {'enabled': False}}]}, {'shots': 1024, 'variance': 0.99847412109375, 'simulator_metadata': [{'batched_shots_optimization': False, 'required_memory_mb': 0, 'method': 'stabilizer', 'active_input_qubits': [0, 1], 'device': 'CPU', 'remapped_qubits': False, 'num_qubits': 2, 'num_clbits': 2, 'time_taken': 0.0034544, 'sample_measure_time': 0.0009696, 'input_qubit_map': [[0, 0], [1, 1]], 'max_memory_mb': 7563, 'measure_sampling': True, 'noise': 'ideal', 'parallel_shots': 1, 'parallel_state_update': 16, 'runtime_parameter_bind': False, 'num_bind_params': 1, 'fusion': {'enabled': False}}]}, {'shots': 1024, 'variance': 0.99847412109375, 'simulator_metadata': [{'batched_shots_optimization': False, 'required_memory_mb': 0, 'method': 'stabilizer', 'active_input_qubits': [0, 1], 'device': 'CPU', 'remapped_qubits': False, 'num_qubits': 2, 'num_clbits': 2, 'time_taken': 0.0034544, 'sample_measure_time': 0.0009696, 'input_qubit_map': [[0, 0], [1, 1]], 'max_memory_mb': 7563, 'measure_sampling': True, 'noise': 'ideal', 'parallel_shots': 1, 'parallel_state_update': 16, 'runtime_parameter_bind': False, 'num_bind_params': 1, 'fusion': {'enabled': False}}]}, {'shots': 1024, 'variance': 0.0, 'simulator_metadata': [{'batched_shots_optimization': False, 'required_memory_mb': 0, 'method': 'stabilizer', 'active_input_qubits': [0, 1], 'device': 'CPU', 'remapped_qubits': False, 'num_qubits': 2, 'num_clbits': 2, 'time_taken': 0.0032652, 'sample_measure_time': 0.0013482, 'input_qubit_map': [[0, 0], [1, 1]], 'max_memory_mb': 7563, 'measure_sampling': True, 'noise': 'ideal', 'parallel_shots': 1, 'parallel_state_update': 16, 'runtime_parameter_bind': False, 'num_bind_params': 1, 'fusion': {'enabled': False}}]}, {'shots': 1024, 'variance': 0.9981536865234375, 'simulator_metadata': [{'batched_shots_optimization': False, 'required_memory_mb': 0, 'method': 'stabilizer', 'active_input_qubits': [0, 1], 'device': 'CPU', 'remapped_qubits': False, 'num_qubits': 2, 'num_clbits': 2, 'time_taken': 0.0032652, 'sample_measure_time': 0.0013482, 'input_qubit_map': [[0, 0], [1, 1]], 'max_memory_mb': 7563, 'measure_sampling': True, 'noise': 'ideal', 'parallel_shots': 1, 'parallel_state_update': 16, 'runtime_parameter_bind': False, 'num_bind_params': 1, 'fusion': {'enabled': False}}]}, {'shots': 1024, 'variance': 0.9981536865234375, 'simulator_metadata': [{'batched_shots_optimization': False, 'required_memory_mb': 0, 'method': 'stabilizer', 'active_input_qubits': [0, 1], 'device': 'CPU', 'remapped_qubits': False, 'num_qubits': 2, 'num_clbits': 2, 'time_taken': 0.0032652, 'sample_measure_time': 0.0013482, 'input_qubit_map': [[0, 0], [1, 1]], 'max_memory_mb': 7563, 'measure_sampling': True, 'noise': 'ideal', 'parallel_shots': 1, 'parallel_state_update': 16, 'runtime_parameter_bind': False, 'num_bind_params': 1, 'fusion': {'enabled': False}}]}])
102+
103+
104+
105+
106+
```python
107+
# post-processing
108+
import matplotlib.pyplot as plt
109+
110+
values = job.result().values
111+
112+
plt.plot(observables_labels, values, '-o')
113+
```
114+
115+
116+
117+
118+
[<matplotlib.lines.Line2D at 0x17b6fd097c0>]
119+
120+
121+
122+
123+
124+
![png](example_files/example_4_1.png)
125+
126+
127+
128+
129+
```python
130+
# Quantum Computing with Qiskit: Introduction and Basic Circuits
131+
132+
from qiskit import QuantumCircuit
133+
from qiskit_aer import AerSimulator
134+
from qiskit.visualization import plot_histogram
135+
136+
# Create a quantum circuit with 2 qubits and 2 classical bits
137+
qc = QuantumCircuit(2, 2)
138+
139+
# Apply a Hadamard gate to the first qubit
140+
qc.h(0)
141+
142+
# Apply a CNOT gate with control qubit 0 and target qubit 1
143+
qc.cx(0, 1)
144+
145+
# Measure both qubits
146+
qc.measure([0, 1], [0, 1])
147+
148+
# Print the circuit
149+
print(qc)
150+
151+
# Create a simulator
152+
simulator = AerSimulator()
153+
154+
# Run the quantum circuit on the simulator backend
155+
job = simulator.run(qc, shots=1000)
156+
157+
# Get the results of the simulation
158+
result = job.result()
159+
160+
# Get the counts of measurement outcomes
161+
counts = result.get_counts(qc)
162+
163+
# Print the results
164+
print("\nMeasurement outcomes:", counts)
165+
166+
# Visualize the results
167+
plot_histogram(counts)
168+
```
169+
170+
┌───┐ ┌─┐
171+
q_0: ┤ H ├──■──┤M├───
172+
└───┘┌─┴─┐└╥┘┌─┐
173+
q_1: ─────┤ X ├─╫─┤M├
174+
└───┘ ║ └╥┘
175+
c: 2/═══════════╩══╩═
176+
0 1
177+
178+
Measurement outcomes: {'11': 496, '00': 504}
179+
180+
181+
182+
183+
184+
185+
![png](example_files/example_5_1.png)
186+
187+
188+
189+
190+
191+
```python
192+
# Quantum Computing with Qiskit: Introduction and Basic Circuits
193+
194+
from qiskit import QuantumCircuit
195+
from qiskit_aer import AerSimulator
196+
from qiskit.visualization import plot_histogram
197+
198+
# Create a quantum circuit with 2 qubits and 2 classical bits
199+
qc = QuantumCircuit(2,2)
200+
201+
# Apply a Hadamard gate to the first qubit
202+
qc.h(0)
203+
204+
# Apply a CNOT gate with control qubit 0 and target qubit 1
205+
qc.cx(0, 1)
206+
207+
# Measure both qubits
208+
qc.measure([0, 1], [0, 1])
209+
210+
# Print the circuit
211+
qc.draw("mpl")
212+
213+
```
214+
215+
216+
217+
218+
219+
![png](example_files/example_6_0.png)
220+
221+
222+
223+
224+
225+
```python
226+
227+
# Create a simulator
228+
simulator = AerSimulator()
229+
230+
# Run the quantum circuit on the simulator backend
231+
job = simulator.run(qc, shots=1000)
232+
233+
# Get the results of the simulation
234+
result = job.result()
235+
236+
# Get the counts of measurement outcomes
237+
counts = result.get_counts(qc)
238+
239+
# Print the results
240+
print("\nMeasurement outcomes:", counts)
241+
242+
# Visualize the results
243+
plot_histogram(counts)
244+
```
245+
246+
247+
Measurement outcomes: {'00': 499, '11': 501}
248+
249+
250+
251+
252+
253+
254+
![png](example_files/example_7_1.png)
255+
256+
257+
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)