Scripted element implementing 2x2 coupler does not seem to behave properly

scripted_element
interconnect
directional_coupler

#1

I have created a 2x2 scripted coupler to get familiar with the scripted elements.
However, when comparing with the native coupler from the interconnect library, the readings on the powermeters are a bit off:
-3.078dBm instead of -3.010dBm
Cross phase of 1.5462rad instead of 1.5708rad

When I check the transfer matrix values from within the script, I get the correct phase and amplitude.
Do you have any idea of what could have gone wrong?

The test I am running looks like this:

Please find below the full script used in the coupler

# Initialize tranfer matrix
freqs = [1.90950610191e+14, 1.95942783007e+14];
nb_freqs = length(freqs);
T = coupling;
R = 1-T;
r = sqrt(R);
t = sqrt(T);
amplitudes = zeros(4, 4);

amplitudes(1, 3) = r;
amplitudes(1, 4) = t;
amplitudes(2, 3) = t;
amplitudes(2, 4) = r;

amplitudes(3, 1) = r;
amplitudes(4, 1) = t;
amplitudes(3, 2) = t;
amplitudes(4, 2) = r;

# Phase matrix (non-dispersive)
phases = zeros(4, 4);
phases(1, 4) = pi/2;
phases(2, 3) = pi/2;
phases(4, 1) = pi/2;
phases(3, 2) = pi/2;

# Transfer matrix elements
trans11=zeros(nb_freqs, 3);
trans12=zeros(nb_freqs, 3);
trans13=zeros(nb_freqs, 3);
trans14=zeros(nb_freqs, 3);
trans21=zeros(nb_freqs, 3);
trans22=zeros(nb_freqs, 3);
trans23=zeros(nb_freqs, 3);
trans24=zeros(nb_freqs, 3);
trans31=zeros(nb_freqs, 3);
trans32=zeros(nb_freqs, 3);
trans33=zeros(nb_freqs, 3);
trans34=zeros(nb_freqs, 3);
trans41=zeros(nb_freqs, 3);
trans42=zeros(nb_freqs, 3);
trans43=zeros(nb_freqs, 3);
trans44=zeros(nb_freqs, 3);

trans11(:,1)=freqs;
trans12(:,1)=freqs;
trans13(:,1)=freqs;
trans14(:,1)=freqs;

trans21(:,1)=freqs;
trans22(:,1)=freqs;
trans23(:,1)=freqs;
trans24(:,1)=freqs;

trans31(:,1)=freqs;
trans32(:,1)=freqs;
trans33(:,1)=freqs;
trans34(:,1)=freqs;

trans41(:,1)=freqs;
trans42(:,1)=freqs;
trans43(:,1)=freqs;
trans44(:,1)=freqs;


## AMPLITUDES
trans11(:,2)=amplitudes(1,1);
trans12(:,2)=amplitudes(1,2);
trans13(:,2)=amplitudes(1,3);
trans14(:,2)=amplitudes(1,4);

trans21(:,2)=amplitudes(2,1);
trans22(:,2)=amplitudes(2,2);
trans23(:,2)=amplitudes(2,3);
trans24(:,2)=amplitudes(2,4);

trans31(:,2)=amplitudes(3,1);
trans32(:,2)=amplitudes(3,2);
trans33(:,2)=amplitudes(3,3);
trans34(:,2)=amplitudes(3,4);

trans41(:,2)=amplitudes(4,1);
trans42(:,2)=amplitudes(4,2);
trans43(:,2)=amplitudes(4,3);
trans44(:,2)=amplitudes(4,4);

## Phases
trans41(:, 3)= phases(4,1);
trans14(:, 3)= phases(1,4);
trans23(:, 3)= phases(2,3);
trans32(:, 3)= phases(3,2);

# PORT MAPPING
setsparameter("port 1", "port 1", "transmission", trans11);
setsparameter("port 1", "port 2", "transmission", trans12);
setsparameter("port 1", "port 3", "transmission", trans13);
setsparameter("port 1", "port 4", "transmission", trans14);

setsparameter("port 2", "port 1", "transmission", trans21);
setsparameter("port 2", "port 2", "transmission", trans22);
setsparameter("port 2", "port 3", "transmission", trans23);
setsparameter("port 2", "port 4", "transmission", trans24);

setsparameter("port 3", "port 1", "transmission", trans31);
setsparameter("port 3", "port 2", "transmission", trans32);
setsparameter("port 3", "port 3", "transmission", trans33);
setsparameter("port 3", "port 4", "transmission", trans34);

setsparameter("port 4", "port 1", "transmission", trans41);
setsparameter("port 4", "port 2", "transmission", trans42);
setsparameter("port 4", "port 3", "transmission", trans43);
setsparameter("port 4", "port 4", "transmission", trans44);