Skip to content

Commit

Permalink
update tutorials (#623)
Browse files Browse the repository at this point in the history
* refactor [examples] - teleportation

* refactor [examples] - teleportation

remove default optional parameters

Co-authored-by: Theodor <theodor.isacsson@gmail.com>

* fix [examples] - gaussian_cloning

* fix [examples] - gate_teleportation

use the .par attribute of a measured parameter

Co-authored-by: Theodor <theodor.isacsson@gmail.com>
  • Loading branch information
sduquemesa and thisac committed Sep 9, 2021
1 parent 724b0a7 commit 717e7a1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 45 deletions.
12 changes: 6 additions & 6 deletions examples/gate_teleportation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
# compare against the expected output
# X(q1/sqrt(2)).F.P(0.5).X(q0/sqrt(0.5)).F.|z>
# not including the corrections
Squeezed(0.1) | q[3]
Fourier | q[3]
Xgate(q[0]) | q[3]
Pgate(0.5) | q[3]
Fourier | q[3]
Xgate(q[1]) | q[3]
Squeezed(0.1) | q[3]
Fourier | q[3]
Xgate(q[0].par) | q[3]
Pgate(0.5) | q[3]
Fourier | q[3]
Xgate(q[1].par) | q[3]
# end circuit

results = eng.run(gate_teleportation)
Expand Down
8 changes: 6 additions & 2 deletions examples/gaussian_cloning.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
eng = sf.Engine(backend="gaussian")
gaussian_cloning = sf.Program(4)

alpha = 0.7+1.2j
r = np.abs(alpha)
phi = np.angle(alpha)

with gaussian_cloning.context as q:
# state to be cloned
Coherent(0.7+1.2j) | q[0]
Coherent(r, phi) | q[0]

# 50-50 beamsplitter
BS = BSgate(pi/4, 0)
Expand Down Expand Up @@ -50,4 +54,4 @@

print("Fidelity of cloned state:", np.mean(f))
print("Mean displacement of cloned state:", np.mean(a))
print("Mean covariance matrix of cloned state:", np.cov([a.real, a.imag]))
print("Mean covariance matrix of cloned state:\n", np.cov([a.real, a.imag]))
84 changes: 47 additions & 37 deletions examples/teleportation.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
#!/usr/bin/env python3
import strawberryfields as sf
from strawberryfields.ops import *
from strawberryfields.utils import scale
from strawberryfields.ops import (
Coherent,
Squeezed,
BSgate,
Xgate,
Zgate,
MeasureX,
MeasureP,
)

import numpy as np
from numpy import pi, sqrt

# initialize engine and program objects
eng = sf.Engine(backend="gaussian")
teleportation = sf.Program(3)

with teleportation.context as q:
psi, alice, bob = q[0], q[1], q[2]

# state to be teleported:
Coherent(1+0.5j) | psi

# 50-50 beamsplitter
BS = BSgate(pi/4, 0)

# maximally entangled states
Squeezed(-2) | alice
Squeezed(2) | bob
BS | (alice, bob)

# Alice performs the joint measurement
# in the maximally entangled basis
BS | (psi, alice)
MeasureX | psi
MeasureP | alice

# Bob conditionally displaces his mode
# based on Alice's measurement result
Xgate(psi.par*sqrt(2)) | bob
Zgate(alice.par*sqrt(2)) | bob
# end circuit

results = eng.run(teleportation)
# view Bob's output state and fidelity
print(results.samples)
print(results.state.displacement([2]))
print(results.state.fidelity_coherent([0, 0, 1+0.5j]))
# set the random seed
np.random.seed(42)

prog = sf.Program(3)

alpha = 1 + 0.5j
r = np.abs(alpha)
phi = np.angle(alpha)

with prog.context as q:
# prepare initial states
Coherent(r, phi) | q[0]
Squeezed(-2) | q[1]
Squeezed(2) | q[2]

# apply gates
BS = BSgate(pi / 4, pi)
BS | (q[1], q[2])
BS | (q[0], q[1])

# Perform homodyne measurements
MeasureX | q[0]
MeasureP | q[1]

# Displacement gates conditioned on
# the measurements
Xgate(sqrt(2) * q[0].par) | q[2]
Zgate(sqrt(2) * q[1].par) | q[2]

eng = sf.Engine("fock", backend_options={"cutoff_dim": 15})
result = eng.run(prog)

# view output state and fidelity
print(result.samples)
print(result.state)
print(result.state.fidelity_coherent([0, 0, alpha]))

0 comments on commit 717e7a1

Please sign in to comment.