qiskit.circuit.QuantumCircuit.initialize¶
- QuantumCircuit.initialize(params, qubits=None)¶
Initialize qubits in a specific state.
Qubit initialization is done by first resetting the qubits to \(|0\rangle\) followed by calling
qiskit.extensions.StatePreparation
class to prepare the qubits in a specified state. Both these steps are included in theqiskit.extensions.Initialize
instruction.- パラメータ
params (str or list or int) –
str: labels of basis states of the Pauli eigenstates Z, X, Y. See
Statevector.from_label()
. Notice the order of the labels is reversed with respect to the qubit index to be applied to. Example label 『01』 initializes the qubit zero to \(|1\rangle\) and the qubit one to \(|0\rangle\).list: vector of complex amplitudes to initialize to.
int: an integer that is used as a bitmap indicating which qubits to initialize to \(|1\rangle\). Example: setting params to 5 would initialize qubit 0 and qubit 2 to \(|1\rangle\) and qubit 1 to \(|0\rangle\).
qubits (QuantumRegister or Qubit or int) –
QuantumRegister: A list of qubits to be initialized [Default: None].
Qubit: Single qubit to be initialized [Default: None].
int: Index of qubit to be initialized [Default: None].
list: Indexes of qubits to be initialized [Default: None].
- 戻り値
a handle to the instruction that was just initialized
- 戻り値の型
サンプル
Prepare a qubit in the state \((|0\rangle - |1\rangle) / \sqrt{2}\).
import numpy as np from qiskit import QuantumCircuit circuit = QuantumCircuit(1) circuit.initialize([1/np.sqrt(2), -1/np.sqrt(2)], 0) circuit.draw()
output:
┌──────────────────────────────┐ q_0: ┤ Initialize(0.70711,-0.70711) ├ └──────────────────────────────┘
Initialize from a string two qubits in the state \(|10\rangle\). The order of the labels is reversed with respect to qubit index. More information about labels for basis states are in
Statevector.from_label()
.import numpy as np from qiskit import QuantumCircuit circuit = QuantumCircuit(2) circuit.initialize('01', circuit.qubits) circuit.draw()
output:
┌──────────────────┐ q_0: ┤0 ├ │ Initialize(0,1) │ q_1: ┤1 ├ └──────────────────┘
Initialize two qubits from an array of complex amplitudes.
import numpy as np from qiskit import QuantumCircuit circuit = QuantumCircuit(2) circuit.initialize([0, 1/np.sqrt(2), -1.j/np.sqrt(2), 0], circuit.qubits) circuit.draw()
output:
┌────────────────────────────────────┐ q_0: ┤0 ├ │ Initialize(0,0.70711,-0.70711j,0) │ q_1: ┤1 ├ └────────────────────────────────────┘