qml.measurements.StateMeasurement¶
-
class
StateMeasurement
(obs=None, wires=None, eigvals=None, id=None)[source]¶ Bases:
pennylane.measurements.measurements.MeasurementProcess
State-based measurement process.
Any class inheriting from
StateMeasurement
should define its ownprocess_state
method, which should have the following arguments:- state (Sequence[complex]): quantum state with a flat shape. It may also have an
optional batch dimension
- wire_order (Wires): wires determining the subspace that
state
acts on; a matrix of dimension \(2^n\) acts on a subspace of \(n\) wires
- wire_order (Wires): wires determining the subspace that
Example:
Let’s create a measurement that returns the diagonal of the reduced density matrix.
>>> class MyMeasurement(StateMeasurement): ... def process_state(self, state, wire_order): ... # use the already defined `qml.density_matrix` measurement to compute the ... # reduced density matrix from the given state ... density_matrix = qml.density_matrix(wires=self.wires).process_state(state, wire_order) ... return qml.math.diagonal(qml.math.real(density_matrix))
We can now execute it in a QNode:
>>> dev = qml.device("default.qubit", wires=2) >>> @qml.qnode(dev) ... def circuit(): ... qml.Hadamard(0) ... qml.CNOT([0, 1]) ... return MyMeasurement(wires=[0]) >>> circuit() tensor([0.5, 0.5], requires_grad=True)
Attributes
Whether or not the MeasurementProcess returns a defined decomposition when calling
expand
.returns an integer hash uniquely representing the measurement process
The Python numeric type of the measurement result.
The wires the measurement process acts on.
Measurement return type.
Whether or not the MeasurementProcess measures in the computational basis.
The wires the measurement process acts on.
-
has_decomposition
¶ Whether or not the MeasurementProcess returns a defined decomposition when calling
expand
.- Type
Bool
-
hash
¶ returns an integer hash uniquely representing the measurement process
- Type
int
-
numeric_type
¶ The Python numeric type of the measurement result.
- Returns
The output numeric type;
int
,float
orcomplex
.- Return type
type
- Raises
QuantumFunctionError – the return type of the measurement process is unrecognized and cannot deduce the numeric type
-
raw_wires
¶ The wires the measurement process acts on.
For measurements involving more than one set of wires (such as mutual information), this is a list of the Wires objects. Otherwise, this is the same as
wires()
-
return_type
¶ Measurement return type.
-
samples_computational_basis
¶ Whether or not the MeasurementProcess measures in the computational basis.
- Type
Bool
-
wires
¶ The wires the measurement process acts on.
This is the union of all the Wires objects of the measurement.
Methods
Returns the gates that diagonalize the measured wires such that they are in the eigenbasis of the circuit observables.
eigvals
()Eigenvalues associated with the measurement process.
expand
()Expand the measurement of an observable to a unitary rotation and a measurement in the computational basis.
map_wires
(wire_map)Returns a copy of the current measurement process with its wires changed according to the given wire map.
process_state
(state, wire_order)Process the given quantum state.
queue
([context])Append the measurement process to an annotated queue.
shape
(device, shots)The expected output shape of the MeasurementProcess.
simplify
()Reduce the depth of the observable to the minimum.
-
diagonalizing_gates
()¶ Returns the gates that diagonalize the measured wires such that they are in the eigenbasis of the circuit observables.
- Returns
the operations that diagonalize the observables
- Return type
List[Operation]
-
eigvals
()¶ Eigenvalues associated with the measurement process.
If the measurement process has an associated observable, the eigenvalues will correspond to this observable. Otherwise, they will be the eigenvalues provided when the measurement process was instantiated.
Note that the eigenvalues are not guaranteed to be in any particular order.
Example:
>>> m = MeasurementProcess(Expectation, obs=qml.PauliX(wires=1)) >>> m.eigvals() array([1, -1])
- Returns
eigvals representation
- Return type
array
-
expand
()¶ Expand the measurement of an observable to a unitary rotation and a measurement in the computational basis.
- Returns
a quantum tape containing the operations required to diagonalize the observable
- Return type
Example:
Consider a measurement process consisting of the expectation value of an Hermitian observable:
>>> H = np.array([[1, 2], [2, 4]]) >>> obs = qml.Hermitian(H, wires=['a']) >>> m = MeasurementProcess(Expectation, obs=obs)
Expanding this out:
>>> tape = m.expand()
We can see that the resulting tape has the qubit unitary applied, and a measurement process with no observable, but the eigenvalues specified:
>>> print(tape.operations) [QubitUnitary(array([[-0.89442719, 0.4472136 ], [ 0.4472136 , 0.89442719]]), wires=['a'])] >>> print(tape.measurements[0].eigvals()) [0. 5.] >>> print(tape.measurements[0].obs) None
-
map_wires
(wire_map)¶ Returns a copy of the current measurement process with its wires changed according to the given wire map.
- Parameters
wire_map (dict) – dictionary containing the old wires as keys and the new wires as values
- Returns
new measurement process
- Return type
-
abstract
process_state
(state, wire_order)[source]¶ Process the given quantum state.
- Parameters
state (Sequence[complex]) – quantum state with a flat shape. It may also have an optional batch dimension
wire_order (Wires) – wires determining the subspace that
state
acts on; a matrix of dimension \(2^n\) acts on a subspace of \(n\) wires
-
queue
(context=<class 'pennylane.queuing.QueuingManager'>)¶ Append the measurement process to an annotated queue.
-
shape
(device, shots)¶ The expected output shape of the MeasurementProcess.
Note that the output shape is dependent on the shots or device when:
The measurement type is either
_Probability
,_State
(fromstate()
) or_Sample
;The shot vector was defined.
For example, assuming a device with
shots=None
, expectation values and variances defineshape=(,)
, whereas probabilities in the qubit model defineshape=(2**num_wires)
wherenum_wires
is the number of wires the measurement acts on.- Parameters
device (pennylane.Device) – a PennyLane device to use for determining the shape
shots (Shots) – object defining the number and batches of shots
- Returns
the output shape
- Return type
tuple
- Raises
QuantumFunctionError – the return type of the measurement process is unrecognized and cannot deduce the numeric type
-
simplify
()¶ Reduce the depth of the observable to the minimum.
- Returns
A measurement process with a simplified observable.
- Return type