I want to easily create a circuit that creates an arbitrary state! Do you have any thoughts? For example ...
python
\frac{|0>+|1>}{\sqrt{2}}
Well, this is easy. It can be created with the following circuit.
But there are tools that can create this state without knowing the Hadamard gate! That is the StateVectorCircuit.
StateVectorCircuit
Let's create a circuit that creates the above state using StateVectorCircuit.
First, import the package to be used.
python
import numpy as np
from qiskit.aqua.circuits import StateVectorCircuit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute
from qiskit import BasicAer
from qiskit.visualization import plot_histogram
Next, define the state you want to create this time
python
state = [1 / np.sqrt(2), 1 / np.sqrt(2)]
Enter this into the StateVectorCircuit.
python
svc = StateVectorCircuit(state)
Two values are stored here.
python
print(svc._num_qubits)
# 1
print(svc._state_vector)
# [0.70710678 0.70710678]
Since the circuit has not been created yet, construct_circuit is performed here.
qc = svc.construct_circuit()
This is the created circuit
Let's actually execute it and see the result.
cr = ClassicalRegister(1)
qc.add_register(cr)
qc.measure([0], [0])
num_shots = 10000
backend = BasicAer.get_backend('qasm_simulator')
results = execute(qc, backend, shots=num_shots).result()
counts = results.get_counts(qc)
plot_histogram(counts1)
HM. It feels good.
By the way, it will normalize without permission, so for example
python
state = [100, 100]
It will also support you. Of course, multiple qubits are fine.
This time, I introduced StateVectorCircuit, which seems to be useful. By the way, if the version of Qiskit you are using is old, you may get an error. Qiskit itself is evolving day by day.
Recommended Posts