# Implementing Noise in simulator for Density Matrix Calculation

Hi everyone,

I’m currently working on a project . Here’s the core of my setup:

backend = pcvl.Simulator(pcvl.NaiveBackend())
input_states = {
pcvl.BasicState([1, 0, 1, 0, 1, 0, 1, 0, 1, 0]): “|00000>”
}
output_states = {
pcvl.BasicState([1, 0, 1, 0, 1, 0, 1, 0, 1, 0]): “|00000>”,
pcvl.BasicState([1, 0, 1, 0, 1, 0, 1, 0, 0, 1]): “|00001>”,
pcvl.BasicState([1, 0, 1, 0, 1, 0, 0, 1, 1, 0]): “|00010>”,
# … (other states)
pcvl.BasicState([0, 1, 0, 1, 0, 1, 0, 1, 0, 1]): “|11110>”
}
Hamiltonian_elem = np.diag([1 if ‘{:05b}’.format(i)[-1] == ‘0’ else -1 for i in range(32)])
for j, value in enumerate(initial_point_2):
param_circuit[j + 5].set_value(value)
backend.set_circuit(VQE)
psi = []
for input_state in input_states:
for output_state in output_states:
psi.append(backend.prob_amplitude(input_state, output_state))

I am trying to figure out how to introduce noise into the backend. The is to get a noise-affected density matrix , I calcualte through the calculation of probability amplitudes. I would greatly appreciate any guidance or resources you could provide on implementing noise processes in a simulator .

Thank you very much for any help.

Andres

Hello @Andres ,

You’re using a `Simulator` to compute probability amplitudes one by one.
In order to inject noise in your simulation, I suggest multiple things:

• compute all probability amplitudes at once (with the `evolve` operator)
• using SLOS instead of Naive will compute faster
• using a `Source` to add noise to an ideal input state

But beware, our source model is creating a mixed state (a `SVDistribution`) which, when evolved gives a mixed state back. Every key of the resulting `SVDistribution` will be a pure state evolution.
Also, if playing around with either `indistinguishability` or `multiphoton_component` (g²), you’ll get annotated photons in the input noisy state, as well as in its evolution. Annotation are kept to allow a further evolution. If you don’t need to re-evolve the same state, you can discard the annotations (see below in the example code). In this case, a single output state probability can (will) get contributions from different components of the noisy state.

``````circuit_size = 10
ideal_input = pcvl.BasicState([1, 0, 1, 0, 1, 0, 1, 0, 1, 0])

# noise parameters
source = pcvl.Source(losses=0.8, indistinguishability=0.85)
noisy_input = source.generate_distribution(ideal_input)

simulator = pcvl.Simulator(pcvl.SLOSBackend())

simulator.set_circuit(VQE)
simulator.set_min_detected_photon_filter(5)  # If you're only insterested by 5-photon coincidences
noisy_evolution = simulator.evolve_svd(noisy_input)

print(f'Performance of the setup, due to the noise: {noisy_evolution["physical_perf"]}')
result = pcvl.anonymize_annotations(noisy_evolution['results'])
for output_state_vector, probability in result.items():
# Do something with the amplitude of ideal_input vs output_state
print(output_state_vector, probability)
for fock_state, amplitude in output_state_vector:
fock_state.clear_annotations()
print(fock_state)
``````

Best wishes,
Eric