Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Shadow to use initial state for faster simulation #644

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions qadence/measurements/shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from qadence.backend import Backend
from qadence.backends.pyqtorch import Backend as PyQBackend
from qadence.blocks import AbstractBlock, KronBlock, chain, kron
from qadence.blocks import AbstractBlock, KronBlock, kron
from qadence.blocks.block_to_tensor import HMAT, IMAT, SDAGMAT
from qadence.blocks.composite import CompositeBlock
from qadence.blocks.primitive import PrimitiveBlock
Expand All @@ -16,7 +16,7 @@
from qadence.measurements.utils import get_qubit_indices_for_op
from qadence.noise import NoiseHandler
from qadence.operations import H, I, SDagger, X, Y, Z
from qadence.types import Endianness
from qadence.types import BackendName, Endianness

pauli_gates = [X, Y, Z]
pauli_rotations = [
Expand Down Expand Up @@ -134,20 +134,38 @@
shadow: list = list()
all_rotations = extract_operators(unitary_ids, circuit.n_qubits)

initial_state = state
backend_name = backend.name if hasattr(backend, "name") else backend.backend.name
if backend_name == BackendName.PYQTORCH:
# run the initial circuit without rotations
# to save computation time
conv_circ = backend.circuit(circuit)
initial_state = backend.run(
circuit=conv_circ,
param_values=param_values,
state=state,
endianness=endianness,
)
all_rotations = [
QuantumCircuit(circuit.n_qubits, rots) if rots else QuantumCircuit(circuit.n_qubits)
for rots in all_rotations
]
else:
all_rotations = [

Check warning on line 154 in qadence/measurements/shadow.py

View check run for this annotation

Codecov / codecov/patch

qadence/measurements/shadow.py#L154

Added line #L154 was not covered by tests
QuantumCircuit(circuit.n_qubits, circuit.block, rots)
if rots
else QuantumCircuit(circuit.n_qubits, circuit.block)
for rots in all_rotations
]

for i in range(shadow_size):
if all_rotations[i]:
rotated_circuit = QuantumCircuit(
circuit.register, chain(circuit.block, all_rotations[i])
)
else:
rotated_circuit = circuit
# Reverse endianness to get sample bitstrings in ILO.
conv_circ = backend.circuit(rotated_circuit)
conv_circ = backend.circuit(all_rotations[i])
batch_samples = backend.sample(
circuit=conv_circ,
param_values=param_values,
n_shots=1,
state=state,
state=initial_state,
noise=noise,
endianness=endianness,
)
Expand Down
Loading