torchquantum
torchquantum copied to clipboard
Add expectation value of a pauli string as a function
Can we add a function that returns a single value when computing the expectation value of some observable Z1Z2? Right now it returns [<Z1> , <Z2>]
as an array.
we can just utilize measure(q_device, n_shots = n_shots)
, measure all the states on computational-basis and post-process the outcome. This feature is available in very common libraries like penny lane, and we should adopt this for our users. The function would be something like the following.
Let's also add the n_shots
as input to the model because this allows the user to do unbiased estimation of the expectation if they so desire.
def exp_val(self, q_device, measure_wires, n_shots=1):
"""
Measure the specified wires on the z-basis and return the expectation value.
When n_shots =1, the function returns +1 or -1
Args:
q_device (QuantumDevice): The quantum device to be used.
wires (list of ints): The wires to be measured.
n_shots (int): The number of shots to be used, defaults to 1.
"""
# measure all wires on the z-basis
measure_bitstring = tq.measure(q_device, n_shots=n_shots)
# calculate the expectation value from the measurement results
exp_val = 0
for bitstring in measure_bitstring:
#add code here
# return exp_val
Hi Dikshant,
The functions are now supported in https://github.com/mit-han-lab/torchquantum/blob/dev/torchquantum/measurement/measurements.py
expval_joint_analytical
and expval_joint_sampling
are the two functions as you described.