Dear Alex,
this is my code which is yours with a slight modification.
#!pip install strawberryfields
import strawberryfields as sf
from strawberryfields import ops
import numpy as np
import matplotlib.pyplot as plt
Function to run the program with specified phase shift values
def run_program(phase_shift_difference):
# Create a Strawberry Fields program with 2 modes
prog = sf.Program(2)
# Start a quantum context
with prog.context as q:
# Initialize the modes to |01>
ops.Fock(0) | q[0]
ops.Fock(1) | q[1]
# First 50:50 beamsplitter
ops.BSgate(np.pi/4, 0) | (q[0], q[1])
######################################
# Apply |1⟩ to the first mode
ops.Fock(1) | q[0]
######################################
# Second phase shifter with the specified value
ops.Rgate(phase_shift_difference ) | q[0]
# Third phase shifter with the specified value
ops.Rgate(np.pi / 2) | q[1]
# Second 50:50 beamsplitter
ops.BSgate(np.pi/4, 0) | (q[0], q[1])
# Measurement in Fock basis
ops.MeasureFock() | [0, 1]
# Create the Strawberry Fields engine
eng = sf.Engine("fock", backend_options={"cutoff_dim": 5})
# Run the engine
result = eng.run(prog)
# Return the measurement results
return result.samples
Values for the phase shift difference
phi_values = np.linspace(0, 2*np.pi, 300)
Collect results for each phase shift difference
probabilities_0 =
probabilities_1 =
for phi in phi_values:
samples = run_program(phi)
prob_0 = np.sum(samples[:, 0] == 1) / len(samples)
prob_1 = np.sum(samples[:, 1] == 1) / len(samples)
probabilities_0.append(prob_0)
probabilities_1.append(prob_1)
Plot the results
plt.plot(phi_values, probabilities_0, label=" q0 =|1⟩“)
plt.plot(phi_values, probabilities_1, label=” q1= |1⟩")
plt.xlabel(“Phase Difference (phi)”)
plt.ylabel(“Probability”)
plt.title(“Effect of Phase Difference in a Mach-Zehnder Interferometer”)
plt.legend()
plt.show()
I removed the extra phase shift as I said before and fixed the value of phase shift of one path to pi/2 and then swept only the other one. you can see the output is random as we expected. but when the phase shift of the second path gets closed to pi/2 the randomness vanishes and the output becomes 1. the randomness also vanishes when the phase shift becomes pi+pi/2. The output is certain again but this time the probablity is zero as expected for a mach-zhender interferometer. you can see that each time either q0 or q1 is |1>.
you can run the simulation several times and see the random pattern changes but the rule above is always true.
if we increase the number of trials and measure the probability by dividing the number of ones to the total number of trials we expect the cosine behaviour of output probability shows itself.
unfortunately I’m not familiar with python so I can’t do it by myself. I need to show this result through simulation.
you also have sent me the squeeze gate test. thank you so much.
I had a problem when compile it. it needs some packages to be installed. I installed this packages but the problem remains.
lida 0.0.10 requires kaleido, which is not installed.
lida 0.0.10 requires python-multipart, which is not installed.
lida 0.0.10 requires uvicorn, which is not installed.
Thank you so much again for your kindness.I don’t forget your favour and I hope I could pay you back.
Best Regards
Mehdi
you can see when