• Members 5 posts

In this question below, I thought B and E are correct because we need a vector in the second argument. But I was wrong. What is the correct use of the initialize() method?

Which two of these can be used to initialize a vector of complex amplitudes in a quantum circuit qc?
A. qc.initialize([0,0,1,1,0])
B. qc.initialize([0,1,1] , [0,1])
C. qc.initialize([0,1], 0)
D. qc.initialize([1,0,0,0])
E. qc.initialize([1,0,0,0] , [0,1])

• Members 12 posts

I would say the correct answer is C, E. A is wrong becase the dimension is not even for a multi-qubit ($dim=2^n$) system. B is wrong for similar reasons. In general, you need to specify qc.initialize( [some state_vec in dim 2^n], qubits=[0,1,2...]). D is a little tricky here. If you already specify the number of qubits in qc = QuantumCircuit(2). D also works. The rest two have correct arguments according to qiskit API doc: qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.initialize.html

I also recommend watching this 1MinuteQiskit tutorial on "How Can I Initialize a Multi-Qubit Statevector to Any State?".

Here is a long example on how to use the initialize( ) method in different scenarios:

import numpy as np
from qiskit import QuantumCircuit
from qiskit.quantum_info import random_statevector

# Specify number of qubits in the system and a random state vector
num_qubit = 2
vec1 = random_statevector(2**1)           # 1-qubit state_vec
vec2 = random_statevector(2**num_qubit)   # 2-qubit state_vec

# Initialize the circuit and draw it
# Case1: Initialize 1-qubit system
qc1 = QuantumCircuit(1)
qc1.initialize(vec1)
qc1.initialize(vec1, 0)
qc1.initialize('0')
qc1.initialize('0', 0)
qc1.draw()

# Case2: Initialize 2-qubit system
qc2 = QuantumCircuit(num_qubit)
qc2.initialize(vec2)
qc2.initialize(vec2, [0,1])
qc2.initialize(vec2, qc2.qubits)
qc2.initialize('01')
# Only initialize a partial system
qc2.initialize(vec1, 0)
qc2.draw()