2
$\begingroup$

I would like to add a noise model to one of the tutorial examples of quantum machine learning in the Qiskit site (PyTorch QGAN implementation).

I used the following codes

sampler_n = AerSampler()
sampler_n.set_options(noise_model=noise_model)

instead of

sampler = Sampler(options={ "shots": shots, "seed": algorithm_globals.random_seed})

Whenever the quantum circuit is well-defined (does not have any parameters), such as

weights_n = algorithm_globals.random.random(qc_zofal.num_parameters)
def create_generator() -> TorchConnector:
    p = qc_zofal.assign_parameters(weights_n)
    qnn = SamplerQNN(
        sampler = sampler_n,
        circuit= p,
        input_params=[],
        weight_params=p.parameters,
        sparse=False,
    )    
    return TorchConnector(qnn, [])

generator_n = create_generator()
print(generator_n)
print(generator_n(torch.tensor([])).reshape(-1,1))   
print(generator.parameters)

Everything is ok. But if I put a parametric circuit in it, I encounter the following error:

WARNING:qiskit_aer.backends.aerbackend:Simulation failed and returned the following error message:
ERROR: Failed to load circuits: Invalid parameterized qobj: instruction param position out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/qiskit/result/result.py in _get_experiment(self, key)
    367             try:
--> 368                 exp = self.results[key]
    369             except IndexError as ex:

IndexError: list index out of range

The above exception was the direct cause of the following exception:

QiskitError                               Traceback (most recent call last)
14 frames
QiskitError: 'Result for experiment "0" could not be found.'

The above exception was the direct cause of the following exception:

QiskitMachineLearningError                Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py in _forward(self, input_data, weights)
    360             results = job.result()
    361         except Exception as exc:
--> 362             raise QiskitMachineLearningError("Sampler job failed.") from exc
    363         result = self._postprocess(num_samples, results)
    364 

QiskitMachineLearningError: 'Sampler job failed.'

Could you please help me How can I fix this error?

And my next question is: How can I access the QNN parameters after each training from pytorchconnector?

$\endgroup$

1 Answer 1

1
$\begingroup$

To add noise in Aer it's done via the backend_options supplied to the primitive. Here is an example using the Aer Estimator primitive but the technique is the same for Aer Sampler.

For the QGAN maybe check out the Qiskit Machine Learning PyTorch qGAN tutorial where you can see there it shows the parameterized circuit it is using.

$\endgroup$
1
  • $\begingroup$ thanks for the reply but the Qiskit tutorial did not consider the noise model. backend_options for the parametric quantum circuit with Aer sampler lead to the above error. sampler_n = AerSampler() sampler_n.set_options(noise_model=noise_model) $\endgroup$
    – Neda
    Commented May 20, 2023 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.