XanaduAI / strawberryfields

Strawberry Fields is a full-stack Python library for designing, simulating, and optimizing continuous variable (CV) quantum optical circuits.
https://strawberryfields.ai
Apache License 2.0
745 stars 187 forks source link

Fix for using `TDMProgram` with `timebins % concurrent_modes != 0` #611

Closed thisac closed 3 years ago

thisac commented 3 years ago

Ready for review, but since it relies on changes in the td3-tdmprogram branch, it's best to wait till that one is merged before merging this PR. Thus the do not merge label.

Context: The TDMProgram will produce wrong results if the number of timebins is not an integer multiple of the number of concurrent modes. This is because the tdmprogram.unroll() function simpy multiplies the unique circuit with the number of shots, not taking into account which qumode has been measured most recently.

Description of the Change:

Benefits: The following now works properly:

theta = [0] * 3
r = 3
shots = 2

prog = sf.TDMProgram(N=2)
with prog.context(theta) as (p, q):
    ops.Xgate(50) | q[1]
    ops.MeasureHomodyne(p[0]) | q[0]
eng = sf.Engine("gaussian")
res = eng.run(prog, shots=shots)
samples = res.samples

print(samples)

outputting

[[[ 0.5660403  49.47315214 49.76274829]]

 [[48.94508121 49.77226491 51.71599116]]]

Possible Drawbacks:

Related GitHub Issues: fixes #607

codecov[bot] commented 3 years ago

Codecov Report

Merging #611 (12b1f14) into master (ec08408) will increase coverage by 0.00%. The diff coverage is 98.59%.

@@           Coverage Diff           @@
##           master     #611   +/-   ##
=======================================
  Coverage   98.56%   98.56%           
=======================================
  Files          77       77           
  Lines        8893     8904   +11     
=======================================
+ Hits         8765     8776   +11     
  Misses        128      128           
Impacted Files Coverage Δ
strawberryfields/program.py 100.00% <ø> (ø)
strawberryfields/tdm/tdmprogram.py 98.24% <98.38%> (+0.08%) :arrow_up:
strawberryfields/api/devicespec.py 95.31% <100.00%> (+0.07%) :arrow_up:
strawberryfields/engine.py 96.49% <100.00%> (ø)
strawberryfields/io.py 96.90% <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 ec08408...12b1f14. Read the comment docs.

fab1an-q commented 3 years ago

Ah, I see. Very clever, thanks!

On Thu., Jul. 22, 2021, 18:07 Theodor, @.***> wrote:

@.**** commented on this pull request.

In strawberryfields/tdm/tdmprogram.py https://github.com/XanaduAI/strawberryfields/pull/611#discussion_r674952351 :

  • @property
  • def concurr_modes(self):
  • """The number of concurrent modes in the program."""
  • return self._concurr_modes

This is just Pythons way of saying that self._concurr_modes is private (and shouldn't be changed manually outside of the class). By having a property, the value can be retrieved but not set (you'd need to add a "setter" method as well to do that, which could involve extra validation etc.).

— You are receiving this because your review was requested. Reply to this email directly, view it on GitHub https://github.com/XanaduAI/strawberryfields/pull/611#discussion_r674952351, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO3BH2CSHCLKGEM6CBKHB2DTZA64JANCNFSM5AY23BIA .

-- xanadu.ai http://xanadu.ai

"To build quantum computers that are useful and available to people everywhere"

This e-mail message is privileged, confidential and subject to copyright. Any unauthorized use or disclosure is prohibited. Le contenu du présent courriel est privilégié, confidentiel et soumis à des droits d'auteur. Il est interdit de l'utiliser ou de le divulguer sans autorisation.