-
Notifications
You must be signed in to change notification settings - Fork 61
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
EntanglementEntropy use circuits with fixed nqubits #448
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for implementing this. I think it works if one uses the entropy as part of circuit (@igres26 can confirm), but it does not work if the entropy is called directly on a state. For example I tried the following:
import numpy as np
from qibo import K
from qibo.callbacks import EntanglementEntropy
# 3 qubit state
state1 = np.random.random(8) + 1j * np.random.random(8)
state1 = K.cast(state1 / np.sqrt(np.sum(np.abs(state1) ** 2)))
# 4 qubit state
state2 = np.random.random(16) + 1j * np.random.random(16)
state2 = K.cast(state2 / np.sqrt(np.sum(np.abs(state2) ** 2)))
entropy = EntanglementEntropy()
ent1 = entropy(state1)
ent2 = entropy(state2)
and I don't get the RuntimeError
. I think the reason is that when entropy is called on a state, it calls set_nqubits
which only calls the nqubits
setter if self._nqubits is None
. So we should either remove the if self._nqubits is None
condition from there and always call the setter or move the new check in the set_nqubits
function. I think the first is a better solution.
Note that in the above example I get a different error:
ValueError: cannot reshape array of size 16 into shape 8
Thanks, should work now. |
Codecov Report
@@ Coverage Diff @@
## master #448 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 84 84
Lines 11640 11645 +5
=========================================
+ Hits 11640 11645 +5
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update, looks good to me now. Both the script from the above post and the following:
from qibo import callbacks, gates, models
entropy = callbacks.EntanglementEntropy()
c = models.Circuit(3)
c.add(gates.CallbackGate(entropy))
c = models.Circuit(4)
c.add(gates.CallbackGate(entropy))
raise the RuntimeError
.
This PR raises an error if the user allocates multiple circuits using different number of qubits but sharing the same
EntanglementEntropy
object. @igres26 could you please check if this PR raises a proper error in your example?