Skip to content

Commit

Permalink
Fix native mcm workflow following dynamic_one_shot refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentmr committed Apr 23, 2024
1 parent 43bed84 commit 694b53b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
22 changes: 19 additions & 3 deletions pennylane_lightning/lightning_qubit/_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,15 @@ def measurement(self, measurementprocess: MeasurementProcess) -> TensorLike:
"""
return self.get_measurement_function(measurementprocess)(measurementprocess)

def measure_final_state(self, circuit: QuantumScript) -> Result:
def measure_final_state(self, circuit: QuantumScript, mid_measurements=None) -> Result:
"""
Perform the measurements required by the circuit on the provided state.
This is an internal function that will be called by the successor to ``lightning.qubit``.
Args:
circuit (QuantumScript): The single circuit to simulate
mid_measurements (None, dict): Dictionary of mid-circuit measurements
Returns:
Tuple[TensorLike]: The measurement results
Expand All @@ -272,6 +273,7 @@ def measure_final_state(self, circuit: QuantumScript) -> Result:
results = self.measure_with_samples(
circuit.measurements,
shots=circuit.shots,
mid_measurements=mid_measurements,
)

if len(circuit.measurements) == 1:
Expand All @@ -285,27 +287,37 @@ def measure_final_state(self, circuit: QuantumScript) -> Result:
# pylint:disable = too-many-arguments
def measure_with_samples(
self,
mps: List[Union[SampleMeasurement, ClassicalShadowMP, ShadowExpvalMP]],
measurements: List[Union[SampleMeasurement, ClassicalShadowMP, ShadowExpvalMP]],
shots: Shots,
mid_measurements=None,
) -> List[TensorLike]:
"""
Returns the samples of the measurement process performed on the given state.
This function assumes that the user-defined wire labels in the measurement process
have already been mapped to integer wires used in the device.
Args:
mps (List[Union[SampleMeasurement, ClassicalShadowMP, ShadowExpvalMP]]):
measurements (List[Union[SampleMeasurement, ClassicalShadowMP, ShadowExpvalMP]]):
The sample measurements to perform
shots (Shots): The number of samples to take
mid_measurements (None, dict): Dictionary of mid-circuit measurements
Returns:
List[TensorLike[Any]]: Sample measurement results
"""
# last N measurements are sampling MCMs in ``dynamic_one_shot`` execution mode
mps = measurements[0 : -len(mid_measurements)] if mid_measurements else measurements
skip_measure = (
any(v == -1 for v in mid_measurements.values()) if mid_measurements else False
)

groups, indices = _group_measurements(mps)

all_res = []
for group in groups:
if skip_measure:
all_res.extend([None] * len(group))
continue
if isinstance(group[0], (ExpectationMP, VarianceMP)) and isinstance(
group[0].obs, SparseHamiltonian
):
Expand Down Expand Up @@ -333,6 +345,10 @@ def measure_with_samples(
res for _, res in sorted(list(enumerate(all_res)), key=lambda r: flat_indices[r[0]])
)

# append MCM samples
if mid_measurements:
sorted_res += tuple(mid_measurements.values())

# put the shot vector axis before the measurement axis
if shots.has_partitioned_shots:
sorted_res = tuple(zip(*sorted_res))
Expand Down
7 changes: 2 additions & 5 deletions pennylane_lightning/lightning_qubit/lightning_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@ def simulate(circuit: QuantumScript, state: LightningStateVector, mcmc: dict = N
if circuit.shots and has_mcm:
mid_measurements = {}
final_state = state.get_final_state(circuit, mid_measurements=mid_measurements)
if any(v == -1 for v in mid_measurements.values()):
return None, mid_measurements
return (
LightningMeasurements(final_state, **mcmc).measure_final_state(circuit),
mid_measurements,
return LightningMeasurements(final_state, **mcmc).measure_final_state(
circuit, mid_measurements=mid_measurements
)
final_state = state.get_final_state(circuit)
return LightningMeasurements(final_state, **mcmc).measure_final_state(circuit)
Expand Down

0 comments on commit 694b53b

Please sign in to comment.