Data recording
Sometimes it is useful to store the snapshots of data during the calculation for some tasks such as debugging. QURI Parts provides the smart way to do it with @recordable
decorator.
Here we create a function that calculates the expectation value of given operators with given quantum state and returns the highest value. Using recorder feature you can store all estimates and retrieve the data outside of the function.
Setup
As an example for demonstration, we first create 10 random operators and a random quantum state. Then we implement a recordable function whose purpose is to estimate the expectation value of the 10 operators on the random state and return the maximal estimate.
import random
import numpy as np
from quri_parts.circuit import QuantumCircuit
from quri_parts.core.operator import Operator, PauliLabel
from quri_parts.core.state.state_helper import quantum_state
qubit_count = 5
# create random state
circuit = QuantumCircuit(qubit_count)
for i in range(qubit_count):
circuit.add_RX_gate(i, random.random() * 2 * np.pi)
circuit.add_RY_gate(i, random.random() * 2 * np.pi)
state = quantum_state(qubit_count, circuit=circuit)
# create 10 random operators
ops = []
for _ in range(10):
coef = random.random()
p_ids = []
p_indices = []
for i in range(qubit_count):
id = random.randint(0, 3)
if id != 0:
p_ids.append(id)
p_indices.append(i)
ops.append(Operator({PauliLabel.from_index_and_pauli_list(p_indices, p_ids): coef}))