0
$\begingroup$

I have a simple parameterized quantum circuit which looks like the image attached. Here is the code used to create it:

from qiskit import QuantumCircuit, Aer, execute
import numpy as np
from qiskit.circuit import ParameterVector, QuantumCircuit

def add_layer(kernel, params, n_qubits):

    qubits = list(range(n_qubits))

    for i in range(n_qubits):
        kernel.rx(params[i], qubits[i])

    for i in range(n_qubits):
        kernel.ry(params[i + n_qubits], qubits[i])

    for i in range(n_qubits):
        kernel.rz(params[i + n_qubits*2], qubits[i])

    for q1, q2 in zip(qubits[0::2], qubits[1::2]):
        kernel.cz(qubits[q1], qubits[q2])

    return kernel


n_qubits = 2
n_samples = 5
simulator = Aer.get_backend('statevector_simulator')

n_parameters = n_qubits*3
parameters = np.random.default_rng(13).uniform(low=0, high=1, size = (n_samples,n_parameters))
np.random.seed(1)

p = ParameterVector('p', length = n_parameters)  

kernel = QuantumCircuit(n_qubits)
kernel = add_layer(kernel, p, n_qubits)

bc = kernel.bind_parameters({p: parameters[0]})

result = execute(bc, simulator).result().get_statevector()

Currently I am only running this for parameters[0] but I would like to run this for all 5 samples in parameters. How do I bind all of them?

Moreover, I would like to calculate the Z expectation value of the final qubit from the statevector with a built in method if there is one available.

Thanks for your help.

$\endgroup$

1 Answer 1

0
$\begingroup$

The method bind_parameters() assigns the parameters to a copy of the circuit and returns it, while keeping the current circuit untouched. So, you can call bind_parameters() multiple times on the same circuit and pass a different set of values each time.

To calculate the expectation value of an operator against a Statevector, you can use Statevector.expectation_value() method

from qiskit.quantum_info.operators.symplectic import Pauli

op = Pauli('ZI')

for m in range(n_samples):
    bc = kernel.bind_parameters({p: parameters[m]})
    result = execute(bc, simulator).result().get_statevector()
    display(result.draw('latex'))

    expectation_value = result.expectation_value(op)
    display(expectation_value)
$\endgroup$
1
  • $\begingroup$ Doing this in a for loop is costly. I am thinking of a QNN workflow where I would like to pass x_train to my parameterized ansatz. How does one deal with that workflow? $\endgroup$ Commented Apr 20, 2023 at 11:44

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.