Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in SymbolicHamiltonian.expectation_from_samples for Qibo 0.2.14 #1535

Closed
chmwzc opened this issue Dec 5, 2024 · 3 comments · Fixed by #1545
Closed

Bug in SymbolicHamiltonian.expectation_from_samples for Qibo 0.2.14 #1535

chmwzc opened this issue Dec 5, 2024 · 3 comments · Fixed by #1545
Assignees
Labels
bug Something isn't working

Comments

@chmwzc
Copy link
Contributor

chmwzc commented Dec 5, 2024

To Reproduce

from qibo import Circuit, gates
from qibo.hamiltonians import SymbolicHamiltonian
from qibo.symbols import Z

circuit = Circuit(3)
circuit.add(gates.M(_i) for _i in (0, 2))
result = circuit(nshots=10)
freq = result.frequencies() # Counter({'00': 10})

ham = SymbolicHamiltonian(Z(2))
qubit_map = [0, 2]

print(ham.expectation_from_samples(freq, qubit_map))

The above code should simply give 1.0, but fails in Qibo 0.2.14. The error message:

Traceback (most recent call last):
  File "/home/wongzc/testing/sample_expectation/qibo_0p2p14_bug/bug.py", line 19, in <module>
    print(ham.expectation_from_samples(freq, qubit_map))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wongzc/miniforge3/envs/qibochem/lib/python3.11/site-packages/qibo/hamiltonians/hamiltonians.py", line 662, in expectation_from_samples
    [
  File "/home/wongzc/miniforge3/envs/qibochem/lib/python3.11/site-packages/qibo/hamiltonians/hamiltonians.py", line 664, in <listcomp>
    * (-1) ** [state[qubit_map[q]] for q in qubits].count("1")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/wongzc/miniforge3/envs/qibochem/lib/python3.11/site-packages/qibo/hamiltonians/hamiltonians.py", line 664, in <listcomp>
    * (-1) ** [state[qubit_map[q]] for q in qubits].count("1")
                     ~~~~~~~~~^^^
IndexError: list index out of range

I believe the problem is caused by the qubit_map argument in SymbolicHamiltonian.expectation_from_samples.

The documentation says that it should be a dictionary:

qubit_map (dict): qubit map.

But the default value is still a list:

if qubit_map is None:
qubit_map = list(range(self.nqubits))

while the code that checks the binary strings from the measurement frequencies will work if qubit_map is a dictionary (like {0: 0, 2:1} for my example above)

* (-1) ** [state[qubit_map[q]] for q in qubits].count("1")

Suggested fix

I would suggest keeping the qubit_map argument for SymbolicHamiltonian as a list (just edit the docs) to keep it consistent across all the different Hamiltonians available, then just change the line above to

* (-1) ** [state[qubit_map.index(q)] for q in qubits].count("1")

@chmwzc chmwzc added the bug Something isn't working label Dec 5, 2024
@MatteoRobbiati
Copy link
Contributor

It seems to me more a mismatch between the dimension of the observable you are measuring and the number of measures added to the circuit. Here you try to measure a 3 particles observable over a state provided by two qubits only.
Adding a measure or reducing the dimension of ham make it work.

Let me know if I did't get your point :)

@chmwzc
Copy link
Contributor Author

chmwzc commented Dec 5, 2024

Thanks for the quick reply! But no, I believe the idea of the qubit_map argument is to allow the use of one set of suitable circuit measurements for obtaining the expectation values for multiple observables simultaneously.

Say for example I want to get the values of Z(0) and Z(2).
Instead of repeating the circuit twice to get the circuit measurements for each individual qubit (once after adding gates.M(0) and again after adding gates.M(2)), I can just run the circuit once after adding both gates.M(0) and gates.M(2), and use qubit_map to indicate that the frequencies of the binary strings measured (e.g. Counter({'00': 10})) are to be mapped to qubits 0 and 2, to get the expectation values of Z(0) and Z(2) respectively.

@BrunoLiegiBastonLiegi
Copy link
Contributor

Thanks @chmwzc, as you already have an idea on how to fix this, would you mind opening a new PR and add me as a reviewer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants