Skip to content

Commit

Permalink
benchmark: Followup and modify benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolarovszki committed Feb 9, 2024
1 parent 60686c2 commit 0c5b9e7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def func():
)
pq.Q(*operation["modes"]) | pq.Beamsplitter(operation["params"][0], 0.0)

pq.Q() | pq.Phaseshifter(np.angle(decomposition.diagonals))
for i, angle in enumerate(np.angle(decomposition.diagonals)):
pq.Q(i) | pq.Phaseshifter(angle)

for operation in reversed(decomposition.direct_operations):
pq.Q(*operation["modes"]) | pq.Beamsplitter(
Expand Down
23 changes: 14 additions & 9 deletions benchmarks/purefock_general_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ def xi():
return 0.3


parameters = [(d, unitary_group.rvs(d)) for d in range(3, 7)]
@pytest.fixture
def cutoff():
return 15


@pytest.fixture
def d():
return 5


@pytest.mark.parametrize("d, interferometer", parameters)
def piquasso_benchmark(benchmark, d, interferometer, r, alpha, xi):
def piquasso_benchmark(benchmark, cutoff, r, alpha, xi, d):
@benchmark
def func():
with pq.Program() as program:
Expand All @@ -54,21 +60,20 @@ def func():
for i in range(d):
pq.Q(i) | pq.Displacement(r=alpha) | pq.Squeezing(r)

pq.Q(all) | pq.Interferometer(interferometer)
pq.Q(all) | pq.Interferometer(unitary_group.rvs(d))

for i in range(d):
pq.Q(i) | pq.Kerr(xi)

simulator_fock = pq.PureFockSimulator(d=d, config=pq.Config(cutoff=d))
simulator_fock = pq.PureFockSimulator(d=d, config=pq.Config(cutoff=cutoff))

simulator_fock.execute(program)


@pytest.mark.parametrize("d, interferometer", parameters)
def strawberryfields_benchmark(benchmark, d, interferometer, r, alpha, xi):
def strawberryfields_benchmark(benchmark, cutoff, r, alpha, xi, d):
@benchmark
def func():
eng = sf.Engine(backend="fock", backend_options={"cutoff_dim": d})
eng = sf.Engine(backend="fock", backend_options={"cutoff_dim": cutoff})

circuit = sf.Program(d)

Expand All @@ -77,7 +82,7 @@ def func():
sf.ops.Dgate(alpha) | q[i]
sf.ops.Sgate(r) | q[i]

sf.ops.Interferometer(interferometer) | tuple(q[i] for i in range(d))
sf.ops.Interferometer(unitary_group.rvs(d)) | tuple(q[i] for i in range(d))

for i in range(d):
sf.ops.Kgate(xi) | q[i]
Expand Down
38 changes: 23 additions & 15 deletions scripts/profile_cvnn_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import numpy as np


PROFILE = False


def create_layer_parameters(d: int):
number_of_beamsplitters: int
if d % 2 == 0:
Expand Down Expand Up @@ -53,8 +56,8 @@ def create_layer_parameters(d: int):


input = 0.01
d = 8
cutoff = 8
d = 2
cutoff = 10

target_state_vector = np.zeros(cutoff_cardinality(cutoff=cutoff, d=d), dtype=complex)
target_state_vector[1] = 1.0
Expand All @@ -64,11 +67,17 @@ def create_layer_parameters(d: int):

parameters = create_layer_parameters(d)

if PROFILE:
profiler_options = tf.profiler.experimental.ProfilerOptions(python_tracer_level=1)
tf.profiler.experimental.start("logdir", options=profiler_options)


with tf.GradientTape() as tape:
with pq.Program() as program:
pq.Q(all) | pq.Vacuum()

pq.Q(all) | pq.Displacement(r=input)
for mode in range(d):
pq.Q(mode) | pq.Displacement(r=input)

i = 0
for col in range(d):
Expand All @@ -87,7 +96,8 @@ def create_layer_parameters(d: int):
for i in range(d - 1):
pq.Q(i) | pq.Phaseshifter(parameters["phis_1"][i])

pq.Q(all) | pq.Squeezing(parameters["squeezings"])
for mode, r in enumerate(parameters["squeezings"]):
pq.Q(mode) | pq.Squeezing(r)

i = 0
for col in range(d):
Expand All @@ -106,8 +116,11 @@ def create_layer_parameters(d: int):
for i in range(d - 1):
pq.Q(i) | pq.Phaseshifter(parameters["phis_2"][i])

pq.Q(all) | pq.Displacement(r=parameters["displacements"])
pq.Q(all) | pq.Kerr(parameters["kappas"])
for mode, r in enumerate(parameters["displacements"]):
pq.Q(mode) | pq.Displacement(r=r)

for mode, xi in enumerate(parameters["kappas"]):
pq.Q(mode) | pq.Kerr(xi=xi)

start_time = time.time()
state = simulator.execute(program).state
Expand All @@ -118,12 +131,6 @@ def create_layer_parameters(d: int):
cost = tf.reduce_sum(tf.abs(target_state_vector - state_vector))


profiler_options = tf.profiler.experimental.ProfilerOptions(
host_tracer_level=3, python_tracer_level=3, device_tracer_level=3
)

# tf.profiler.experimental.start("logdir", options=profiler_options)

flattened_parameters = (
parameters["thetas_1"]
+ parameters["phis_1"]
Expand All @@ -137,7 +144,8 @@ def create_layer_parameters(d: int):
start_time = time.time()
gradient = tape.gradient(cost, flattened_parameters)

print("JACOBIAN CALCULATION TIME:", time.time() - start_time)
print("JACOBIAN SHAPE:", [g.numpy() for g in gradient])
print("GRADIENT CALCULATION TIME:", time.time() - start_time)
print("GRADIENT:", [g.numpy() for g in gradient])

# tf.profiler.experimental.stop()
if PROFILE:
tf.profiler.experimental.stop()

0 comments on commit 0c5b9e7

Please sign in to comment.