SampledExpectation layer seems to recompute the same circuits for different measurement ops
See original GitHub issueExample
c = cirq.Circuit()
qubits = cirq.GridQubit.rect(2, 1)
c.append(cirq.rz(0.)(qubits[0]))
c.append(cirq.H(qubits[1]))
sampled_expectation_layer = tfq.layers.SampledExpectation()
output = sampled_expectation_layer(
[c],
operators=[cirq.Z(qubits[0]), cirq.Z(qubits[1]), cirq.Z(qubits[0]) * cirq.Z(qubits[1])],
repetitions=1)
print(output) # we have [1, -1, 1] sometimes
assert output[0,0]*output[0,1] == output[0,2] # and this may fail
It is unnatural to me that, when repetitions=1 (1 sample), we have inconsistent measurement results between Z1, Z2 and Z1*Z2.
And from code here https://github.com/tensorflow/quantum/blob/db2eac4e598f614a8c0e157c56313a23ef1943fb/tensorflow_quantum/core/ops/batch_util.py#L554-L561, this may be due to that each circuit, batch, measurement ops combination is simulated separately on backends. If it is so, it is not only unnatural but also inefficient since one can measure all ops for one circuit simulation.
My understanding may be wrong somewhere, any ideas on this?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
tfq.layers.SampledExpectation - Quantum - TensorFlow
A layer that calculates a sampled expectation value. ... computed using measurement results sampled from the input circuit. ... Here, different cirq.Circuit ......
Read more >Evaluating expectation values of operators in Qiskit
The operators in Qiskit Aqua allow the evaluation of expectation values both exactly (via matrix multiplication) or on shot-based sampling ...
Read more >Shots and Circuit Repetitions: Developing the Expectation ...
The solution is to run the same quantum circuit many times and see what results occur most commonly. The average result of a...
Read more >API-doc — Mitiq 0.21.0 documentation
The generated circuit consists of depth layers of random qubit permutations followed by random two-qubit gates that are sampled from the Haar measure...
Read more >arXiv:2003.02989v2 [quant-ph] 26 Aug 2021
We supply the expectation op with a tensor of parame- terized circuits, a list of ... the code below that quantum layers can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks for raising this issue. I’m going to refer to the examples from the documentation here: https://www.tensorflow.org/quantum/api_docs/python/tfq/layers/SampledExpectation
The output is defined as:
output[i][j] = <psi(\theta_i) | OP_j | psi(\theta_i) >. In your case this means the circuit will be evaluated three different times, once with<psi | Z_0 | psi >,<psi | Z_1 | psi >,<psi | Z_0 Z_1 | psi >, wherepsiis made from RZ and H. Each of these outputs is estimated using only 1 sample per term.These quantities are all simultaneously observable and can in theory all be estimated at the same time. However this is not true in all cases with more complex operators. Consider the case where each
psi_iis totally different (in structure and not just parameter value) from one another and eachop_jis totally different from one another. Then we have to estimate each one entirely independently of one another because they are not simultaneously observable. In order to accommodate that case and not provide ambiguous behavior (in terms of how we do sampling) between the simultaneous observable vs not simultaneous observable case, we opted to go with this behavior in order to be consistent everywhere.The library snippet you linked actually isn’t getting run in your program and is designed for use with
py_functionswhen one needs the Cirq backend (and not the qsim backend). This is usually only the case when running through theengineor another exotic sampler interface in Cirq.Does this clear things up ?
This issue has not had any activity in a month. Is it stale ?