RegRefError: Trying to act on the same subsystem more than once

I’m new to StrawberryFields. I’ve completed the X8 tutorial and am now working the Squeezer Characterization tutorial. Here is the code, should look familiar:

import numpy as np

from numpy.core.fromnumeric import swapaxes

import strawberryfields as sf

def run_job(





    n_samples=500000, ):

    prog = sf.Program(n_spatial_modes*2, name=job_name)

    # If no unitary is provided, default to the identity

    if unitary is None:

        unitary = np.identity(n_spatial_modes)


    # If no squeezing amplitudes are provided, default to all of them being off

    if squeezing_amplitudes is None:

        squeezing_amplitudes = [0]*n_spatial_modes

    with prog.context as q:

        for i in range(n_spatial_modes):

            sf.ops.S2gate(squeezing_amplitudes[i]) | (q[i], q[i*n_spatial_modes])

        for qumodes in (q[:n_spatial_modes], q[n_spatial_modes:]):

            sf.ops.Interferometer(unitary) | qumodes

        sf.ops.MeasureFock() | q

    eng = sf.RemoteEngine("X8_01")

    return, shots=n_samples, disable_port_permutation=True)

# collect sample that allow us to characterize the squeezer

squeezer0 = run_job(job_name="squeezer0_char", squeezing_amplitudes=[1, 0, 0, 0])

# Analysis


# The samples span 4 spatial x 2 frequency = 8 detector channels.

# we calculate the mean photon number of each

n_means = np.mean(squeezer0.samples, axis=0)

print(f"The mean photon numbers <n> of each channel are: \n{n_means}")

Upon executing the code I get this Traceback:

(qenv) C:\Users\cfernandez37\Documents\Quantum\Xanadu>c:/Users/cfernandez37/Documents/Quantum/Xanadu/qenv/Scripts/python.exe c:/Users/cfernandez37/Documents/Quantum/Xanadu/SqueezerCharacterization/
Traceback (most recent call last):
  File "c:/Users/cfernandez37/Documents/Quantum/Xanadu/SqueezerCharacterization/", line 34, in <module>
    squeezer0 = run_job(job_name="squeezer0_char", squeezing_amplitudes=[1, 0, 0, 0])
  File "c:/Users/cfernandez37/Documents/Quantum/Xanadu/SqueezerCharacterization/", line 24, in run_job
    sf.ops.S2gate(squeezing_amplitudes[i]) | (q[i], q[i*n_spatial_modes])
  File "c:\Users\cfernandez37\Documents\Quantum\Xanadu\qenv\lib\site-packages\strawberryfields\", line 142, in __or__
    reg = pu.Program_current_context.append(self, reg)
  File "c:\Users\cfernandez37\Documents\Quantum\Xanadu\qenv\lib\site-packages\strawberryfields\", line 427, in append
    reg = self._test_regrefs(reg)
  File "c:\Users\cfernandez37\Documents\Quantum\Xanadu\qenv\lib\site-packages\strawberryfields\", line 410, in _test_regrefs
    raise RegRefError("Trying to act on the same subsystem more than once.")
strawberryfields.program_utils.RegRefError: Trying to act on the same subsystem more than once.

I’ve tried searching the internet, the Slack channel, and this forum, and all I can find is the Docs page forRegRefError.

Can someone help me debug what I did wrong? I was able to activate my API key and work through the X8 tutorial with minimal issues.

Case closed. I mis-typed line 24.

sf.ops.S2gate(squeezing_amplitudes[i]) | (q[i], q[i*n_spatial_modes])

should be
sf.ops.S2gate(squeezing_amplitudes[i]) | (q[i], q[i + n_spatial_modes])
Where the * should be a +.

1 Like

Glad you solved it @cfernandez. Don’t hesitate to open another thread if you have any questions!

1 Like