Skip to content

Advance: Custom state preparation

Vu Tuan Hai edited this page Apr 10, 2024 · 2 revisions

In the below, we use qoop to define a quantum compilation process:

$U(\theta)V^{\dagger}=I$

where $U(\theta)$ is a parameterized quantum circuit and $V$ is the state which need to prepare.

Step 1. We use the QuantumStatePreparetion class from qoop.compilation and two modules ansatz, state.

from qoop.compilation.qsp import QuantumStatePreparation
from qoop.core import ansatz, state
import qiskit

Step 2. Defining $U(\theta)$ as function $f:\mathbb{N}\rightarrow qiskit.QuantumCircuit$. Note that this circuit must be able to bind parameter values.

#define a custom ansatz
def custom_ansatz(num_qubits: int) -> qiskit.QuantumCircuit:
    qc = qiskit.QuantumCircuit(num_qubits)
    thetas = qiskit.circuit.ParameterVector(
        'theta', 2 * num_qubits)
    j = 0
    for i in range(num_qubits):
        qc.rx(thetas[j], i)
        qc.rz(thetas[j + 1], i)
        j += 2
    return qc    

1

Step 3. We create a compiler object that includes $U$ and $V^{\dagger}$. The function state.w(numa_qubits) returns W state stored in a circuit, note that we need to put $V^{\dagger}$, therefore the function inverse() is applied.

#run the compiler  
num_qubits = 2
compiler = QuantumStatePreparation(
    u = custom_ansatz(num_qubits)
    target_state = state.w(num_qubits).inverse()
)

Step 4. Call fit() method. We provide few options in this method, the user can leave it blank to use the default hyperparameter, declare the existing options or self-defining as above.

compiler.fit(
    num_steps = 100, 
    optimizer = 'sgd', 
    metrics_func = [
        'loss_basic', 
        'compilation_trace_fidelities'
    ]
)
#plot figure
compiler.plot()

2