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

EntanglementEntropy use circuits with fixed nqubits #448

Merged
merged 2 commits into from
Aug 2, 2021
Merged

Conversation

scarrazza
Copy link
Member

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?

@scarrazza scarrazza requested review from stavros11 and igres26 July 30, 2021 10:38
@scarrazza scarrazza changed the title entropy EntanglementEntropy use circuits with fixed nqubits Jul 30, 2021
Copy link
Member

@stavros11 stavros11 left a 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

src/qibo/core/callbacks.py Outdated Show resolved Hide resolved
@scarrazza
Copy link
Member Author

Thanks, should work now.

@codecov
Copy link

codecov bot commented Jul 30, 2021

Codecov Report

Merging #448 (1435bbe) into master (195aa16) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #448   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           84        84           
  Lines        11640     11645    +5     
=========================================
+ Hits         11640     11645    +5     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/qibo/core/callbacks.py 100.00% <100.00%> (ø)
src/qibo/tests/test_core_callbacks.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 195aa16...1435bbe. Read the comment docs.

Copy link
Member

@stavros11 stavros11 left a 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.

@scarrazza scarrazza merged commit 02994be into master Aug 2, 2021
@scarrazza scarrazza deleted the entronqubits branch September 18, 2021 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants