# sf.utils.extract_unitary¶

extract_unitary(prog, cutoff_dim, vectorize_modes=False, backend='fock')[source]

Numerical array representation of a unitary quantum circuit.

Note that the circuit must only include operations of the strawberryfields.ops.Gate class.

• If vectorize_modes=True, it returns a matrix.

• If vectorize_modes=False, it returns an operator with $$2N$$ indices, where N is the number of modes that the Program is created with. Adjacent indices correspond to output-input pairs of the same mode.

Example:

This shows the Hong-Ou-Mandel effect by extracting the unitary of a 50/50 beamsplitter, and then computing the output given by one photon at each input (notice the order of the indices: $$[out_1, in_1, out_2, in_2,\dots]$$). The result tells us that the two photons always emerge together from a random output port and never one per port.

>>> prog = sf.Program(num_subsystems=2)
>>> with prog.context as q:
>>>     BSgate(np.pi/4) | q
>>> U = extract_unitary(prog, cutoff_dim=3)
>>> print(abs(U[:,1,:,1])**2)
[[0.  0.  0.5]
[0.  0.  0. ]
[0.5 0.  0. ]])

Parameters
• prog (Program) – quantum program

• cutoff_dim (int) – dimension of each index

• vectorize_modes (bool) – If True, reshape input and output modes in order to return a matrix.

• backend (str) – the backend to build the unitary; 'fock' (default) and 'tf' are supported

Returns

numerical array of the unitary circuit

as a NumPy ndarray ('fock' backend) or as a TensorFlow Tensor ('tf' backend)

Return type

array, tf.Tensor

Raises

TypeError – if the operations used to construct the circuit are not all unitary