Ancillary Subsystem Measurement after evolve of an density operator

I would like to measure the ancillae to know about the information of the system density matrix. I have a system density matrix and I would like to evolve through a unitary operator after adding some ancillae qubits. Further I would like to trace out the enviornment like all the ancillae qubits and wants to evaluate the purity of the system density matrix. Here when I would like to impliment this things in pennylane how to perform indirect measurement towards ancillae qubits. I am attaching my code please have a look.

devRho = qml.device("default.mixed", wires=n)
@qml.qnode(devRho)
def processRho(thetas, rho):
    '''
   Input is parameter of thetas of unitary, and rho is the system enviornment density matrix where wire 0 is for system and rest for the enviornment.
  
    '''
    qml.QubitDensityMatrix(rho, wires=wireList)
    # qml.adjoint(qml.SpecialUnitary(thetas, wires=wireList))
    # qml.ApproxTimeEvolution(H, tau, 100)
    qml.SpecialUnitary(thetas, wires=wireList)
    return qml.density_matrix([0])

The purity function for that theta is follows :

@qml.qnode(devRho)
def purity(thetas):
    '''
    Initializes a density matrix and takes its purity.
    '''
    qml.QubitDensityMatrix(processRho(thetas, rho), wires=0)
    return qml.purity(0)

def cost(thetas):
    '''
    Computes linear entropy from purity.
    '''
    return 1 - purity(thetas)

My point is we know measurment is required to know the information about the system qubit before tracing out ancillae so when I need to perform the measurement and through which operator I need to perform measurement.

Hey @SUDHIR_KUMAR_SAHOO,

I’m not sure I understand what you’re trying to do :thinking:. Instead of having a dedicated purity function, though, you can use a transform from the qml.qinfo module (qml.qinfo — PennyLane 0.35.1 documentation):

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

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

print(qml.qinfo.purity(circuit, wires=[0])(np.pi / 2))
0.5

Let me know if that helps!