-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add state initialization implementation in initilization.py
- Loading branch information
1 parent
ec08f38
commit 8b73fc1
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import math | ||
from qiskit import QuantumCircuit, transpile | ||
from qiskit.providers.basic_provider import BasicSimulator | ||
|
||
|
||
############################################################### | ||
# Make a quantum circuit for state initialization. | ||
############################################################### | ||
circuit = QuantumCircuit(4, 4, name="initializer_circ") | ||
|
||
desired_vector = [ | ||
1 / math.sqrt(4) * complex(0, 1), | ||
1 / math.sqrt(8) * complex(1, 0), | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
1 / math.sqrt(8) * complex(1, 0), | ||
1 / math.sqrt(8) * complex(0, 1), | ||
0, | ||
0, | ||
0, | ||
0, | ||
1 / math.sqrt(4) * complex(1, 0), | ||
1 / math.sqrt(8) * complex(1, 0), | ||
] | ||
|
||
circuit.initialize(desired_vector, [0, 1, 2, 3]) | ||
|
||
circuit.measure([0, 1, 2, 3], [0, 1, 2, 3]) | ||
|
||
print(circuit) | ||
|
||
############################################################### | ||
# Execute on a simulator backend. | ||
############################################################### | ||
shots = 10000 | ||
|
||
# Desired vector | ||
print("Desired probabilities: ") | ||
print([format(abs(x * x), ".3f") for x in desired_vector]) | ||
|
||
# Initialize on local simulator | ||
sim_backend = BasicSimulator() | ||
job = sim_backend.run(transpile(circuit, sim_backend), shots=shots) | ||
result = job.result() | ||
|
||
counts = result.get_counts(circuit) | ||
|
||
qubit_strings = [format(i, "04b") for i in range(2**4)] | ||
print("Probabilities from simulator: ") | ||
print([format(counts.get(s, 0) / shots, ".3f") for s in qubit_strings]) |