# qml.qinfo.transforms.purity¶

purity(tape, wires, **kwargs)[source]

Compute the purity of a QuantumTape returning state().

$\gamma = \text{Tr}(\rho^2)$

where $$\rho$$ is the density matrix. The purity of a normalized quantum state satisfies $$\frac{1}{d} \leq \gamma \leq 1$$, where $$d$$ is the dimension of the Hilbert space. A pure state has a purity of 1.

It is possible to compute the purity of a sub-system from a given state. To find the purity of the overall state, include all wires in the wires argument.

Parameters
Returns

The transformed circuit as described in qml.transform. Executing this circuit will provide the purity in the form of a tensor.

Return type

qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]

Example

dev = qml.device("default.mixed", wires=2)

@qml.qnode(dev)
def noisy_circuit(p):
qml.CNOT(wires=[0, 1])
qml.BitFlip(p, wires=0)
qml.BitFlip(p, wires=1)
return qml.state()

@qml.qnode(dev)
def circuit(x):
qml.IsingXX(x, wires=[0, 1])
return qml.state()

>>> purity(noisy_circuit, wires=[0, 1])(0.2)
0.5648000000000398
>>> purity(circuit, wires=[0])(np.pi / 2)
0.5
>>> purity(circuit, wires=[0, 1])(np.pi / 2)
1.0


Using PennyLane

Release news

Development

API

Internals