0
$\begingroup$

I need to repeatedly input one random quantum state after another into a quantum circuit that has been designed and assigned parameters, and these quantum states will be used as input. I have tried

Statevector()

https://qiskit.org/documentation/locale/ja_JP/stubs/qiskit.quantum_info.Statevector.html?highlight=statevector#qiskit.quantum_info.Statevector

Custom()

https://qiskit.org/documentation/stable/0.19/stubs/qiskit.aqua.components.initial_states.Custom.html

initialize()

https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.initialize.html?highlight=initialize#qiskit.circuit.QuantumCircuit.initialize

, but I found Although these functions can generate new state vectors, they cannot be inserted into already designed quantum circuits, resulting in the same circuit results every time. How should I design it? The three functions mentioned above lack the inplace property and cannot generate a new circuit copy.

{ParameterVectorElement(aa[0]): 0.2379597082677889, ParameterVectorElement(aa[1]): 1.4010239667773599, ParameterVectorElement(aa[2]): 0.10401258494998716, ParameterVectorElement(aa[3]): 0.3326296026269176, ParameterVectorElement(bb[0]): 1.6443757859109702, ParameterVectorElement(bb[1]): 0.8877583818970967, ParameterVectorElement(bb[2]): 0.07150986168780663, ParameterVectorElement(bb[3]): 1.720576457948209, ParameterVectorElement(cc[0]): 1.9974865102845014, ParameterVectorElement(cc[1]): 1.4917958623436192, ParameterVectorElement(cc[2]): 0.7199744574903324, ParameterVectorElement(cc[3]): 2.635969165530908}
WARNING:qiskit.aqua.components.initial_states.custom:The provided state_vector is ignored in favor of the provided custom circuit.
ParameterView([]) [0.00449711 0.         0.         ... 0.         0.         0.        ]
C:\Users\15691\AppData\Local\Temp\ipykernel_347040\162399.py:121: DeprecationWarning: The return type of saved density matrices has been changed from a `numpy.ndarray` to a `qiskit.quantum_info.DensityMatrix` as of qiskit-aer 0.10. Accessing numpy array attributes is deprecated and will result in an error in a future release. To continue using saved result objects as arrays you can explicitly cast them using  `np.asarray(object)`.
  t = np.array(density_matrix.real)
[[0.09660034 0.09656075 0.11104839 0.11262035 0.09307498 0.09300402
  0.10599975 0.10750024]
 [0.09656075 0.0984683  0.11262035 0.11646331 0.09300402 0.0948085
  0.10750024 0.11116849]
 [0.11104839 0.11262035 0.1565212  0.16014388 0.10599975 0.10750024
  0.14853878 0.15202993]
 [0.11262035 0.11646331 0.16014388 0.16699589 0.10750024 0.11116849
  0.15202993 0.15858678]
 [0.09307498 0.09300402 0.10599975 0.10750024 0.09142649 0.09132274
  0.10308861 0.10454789]
 [0.09300402 0.0948085  0.10750024 0.11116849 0.09132274 0.09306052
  0.10454789 0.10811539]
 [0.10599975 0.10750024 0.14853878 0.15202993 0.10308861 0.10454789
  0.14355167 0.14698168]
 [0.10750024 0.11116849 0.15202993 0.15858678 0.10454789 0.10811539
  0.14698168 0.15337558]]
{ParameterVectorElement(aa[0]): 0.2379597082677889, ParameterVectorElement(aa[1]): 1.4010239667773599, ParameterVectorElement(aa[2]): 0.10401258494998716, ParameterVectorElement(aa[3]): 0.3326296026269176, ParameterVectorElement(bb[0]): 1.6443757859109702, ParameterVectorElement(bb[1]): 0.8877583818970967, ParameterVectorElement(bb[2]): 0.07150986168780663, ParameterVectorElement(bb[3]): 1.720576457948209, ParameterVectorElement(cc[0]): 1.9974865102845014, ParameterVectorElement(cc[1]): 1.4917958623436192, ParameterVectorElement(cc[2]): 0.7199744574903324, ParameterVectorElement(cc[3]): 2.635969165530908}
WARNING:qiskit.aqua.components.initial_states.custom:The provided state_vector is ignored in favor of the provided custom circuit.
ParameterView([]) [0.01146697 0.         0.         ... 0.         0.         0.        ]
[[0.09660034 0.09656075 0.11104839 0.11262035 0.09307498 0.09300402
  0.10599975 0.10750024]
 [0.09656075 0.0984683  0.11262035 0.11646331 0.09300402 0.0948085
  0.10750024 0.11116849]
 [0.11104839 0.11262035 0.1565212  0.16014388 0.10599975 0.10750024
  0.14853878 0.15202993]
 [0.11262035 0.11646331 0.16014388 0.16699589 0.10750024 0.11116849
  0.15202993 0.15858678]
 [0.09307498 0.09300402 0.10599975 0.10750024 0.09142649 0.09132274
  0.10308861 0.10454789]
 [0.09300402 0.0948085  0.10750024 0.11116849 0.09132274 0.09306052
  0.10454789 0.10811539]
 [0.10599975 0.10750024 0.14853878 0.15202993 0.10308861 0.10454789
  0.14355167 0.14698168]
 [0.10750024 0.11116849 0.15202993 0.15858678 0.10454789 0.10811539
  0.14698168 0.15337558]]
{ParameterVectorElement(aa[0]): 0.2379597082677889, ParameterVectorElement(aa[1]): 1.4010239667773599, ParameterVectorElement(aa[2]): 0.10401258494998716, ParameterVectorElement(aa[3]): 0.3326296026269176, ParameterVectorElement(bb[0]): 1.6443757859109702, ParameterVectorElement(bb[1]): 0.8877583818970967, ParameterVectorElement(bb[2]): 0.07150986168780663, ParameterVectorElement(bb[3]): 1.720576457948209, ParameterVectorElement(cc[0]): 1.9974865102845014, ParameterVectorElement(cc[1]): 1.4917958623436192, ParameterVectorElement(cc[2]): 0.7199744574903324, ParameterVectorElement(cc[3]): 2.635969165530908}
$\endgroup$
1
  • $\begingroup$ consider control flow, WhileLoopOp or ForLoopOp, and random_statevector, for the task, will make your life easier. and run the circuit with .run(qc) $\endgroup$
    – poig
    Commented Jun 27, 2022 at 21:51

2 Answers 2

1
$\begingroup$

You can use QuantumCircuit.compose() method to prepend an Initialize instruction to your circuit. compose() takes a parameter called inplace. If True, the circuit will be modified. Otherwise, a new circuit will be created and returned.

from qiskit import QuantumCircuit
from qiskit.extensions import Initialize
from qiskit.quantum_info import random_statevector
from IPython.display import display

# Create a sample 2-qubit circuit:
circ = QuantumCircuit(2)
circ.h(0)
circ.cx(0, 1)

# Repeatedly initialize the circuit with a random quantum state:
for m in range(5):
    # Create a 2-qubit random statevector:
    state = random_statevector(2 ** 2)
    # Create an "Initialize" instruction and prepend it to the circuit:
    new_circ = circ.compose(Initialize(state), front = True, inplace = False)
    print('Circuit No.', m + 1)
    display(new_circ.draw(fold = -1))

The result:

Circuit No. 1
     ┌────────────────────────────────────────────────────────────────────────────────────┐┌───┐     
q_0: ┤0                                                                                   ├┤ H ├──■──
     │  Initialize(-0.58349+0.2304j,0.35503+0.12377j,-0.31208+0.16129j,-0.36705-0.45493j) │└───┘┌─┴─┐
q_1: ┤1                                                                                   ├─────┤ X ├
     └────────────────────────────────────────────────────────────────────────────────────┘     └───┘
Circuit No. 2
     ┌─────────────────────────────────────────────────────────────────────────────────────┐┌───┐     
q_0: ┤0                                                                                    ├┤ H ├──■──
     │  Initialize(-0.49122+0.28218j,0.22847-0.11786j,0.10971+0.66447j,-0.084787-0.39018j) │└───┘┌─┴─┐
q_1: ┤1                                                                                    ├─────┤ X ├
     └─────────────────────────────────────────────────────────────────────────────────────┘     └───┘
Circuit No. 3
     ┌───────────────────────────────────────────────────────────────────────────────────────┐┌───┐     
q_0: ┤0                                                                                      ├┤ H ├──■──
     │  Initialize(0.41866+0.81892j,0.1204-0.051467j,0.060036-0.00024577j,-0.13383-0.33975j) │└───┘┌─┴─┐
q_1: ┤1                                                                                      ├─────┤ X ├
     └───────────────────────────────────────────────────────────────────────────────────────┘     └───┘
Circuit No. 4
     ┌────────────────────────────────────────────────────────────────────────────────────┐┌───┐     
q_0: ┤0                                                                                   ├┤ H ├──■──
     │  Initialize(-0.26621+0.53027j,-0.13784+0.57166j,0.1804-0.15494j,-0.30107-0.39365j) │└───┘┌─┴─┐
q_1: ┤1                                                                                   ├─────┤ X ├
     └────────────────────────────────────────────────────────────────────────────────────┘     └───┘
Circuit No. 5
     ┌───────────────────────────────────────────────────────────────────────────────────────┐┌───┐     
q_0: ┤0                                                                                      ├┤ H ├──■──
     │  Initialize(-0.043747+0.10925j,0.23058+0.92953j,0.21934+0.00061991j,0.095399-0.1084j) │└───┘┌─┴─┐
q_1: ┤1                                                                                      ├─────┤ X ├
     └───────────────────────────────────────────────────────────────────────────────────────┘     └───┘
$\endgroup$
0
$\begingroup$

consider control flow, WhileLoopOp or ForLoopOp, and random_statevector, for the task, will make your life easier. and run the circuit with .run(qc)

Example:

from qiskit.quantum_info import random_statevector
from qiskit.circuit import ForLoopOp,QuantumCircuit
from qiskit.providers.aer import AerSimulator

def rand_init(qubit_num):
    loop_qc = QuantumCircuit(2)
    loop_qc.initialize(random_statevector(2**qubit_num),[0,1])
    return loop_qc


qc = QuantumCircuit(2, 2)

# method 1
#loop_op = ForLoopOp(range(4),None,rand_init(qc.num_qubits))
#qc.append(loop_op,[0,1])

# method 2
#qc.for_loop(range(4),None,rand_init(qc.num_qubits), [0,1], [])

# method 3(recommend)
with qc.for_loop(range(5)) as i:
    qc.append(rand_init(qc.num_qubits),[0,1])
    #extra_example#
    #qc.measure(0, 0)
    #qc.break_loop().c_if(0, True)

qc.draw(cregbundle=False)

output:
     ┌───────────┐
q_0: ┤0          ├
     │  For_loop │
q_1: ┤1          ├
     └───────────┘
c_0: ═════════════
                  
c_1: ═════════════

qc.measure_all(add_bits=False)
job = AerSimulator(method="statevector").run(qc) 
job.result().get_counts()
output:
{'01': 188, '11': 165, '00': 400, '10': 271}

For more example, check the qiskit test file: https://github.com/Qiskit/qiskit-terra/blob/ea0266769802a57de2ca823b1b996b668ec81178/test/python/circuit/test_control_flow.py

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.