1
$\begingroup$

While implementing a code, getting the below error:

AttributeError                            Traceback (most recent call last)
<ipython-input-22-d35ba980e0c5> in <module>
     23     optimizer.zero_grad()
     24 
---> 25     output = model(X_tr)
     26     optimizer.zero_grad()
     27     loss = loss_func(output)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

<ipython-input-20-fe528190183d> in forward(self, x)
     13 
     14     def forward(self,x):
---> 15         h = self.f(x)
     16         return self.g(h)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

<ipython-input-18-f3b96ecb25f3> in forward(self, x)
     34         x = self.fc(x[:, -1, :])
     35         # out.size() --> 100, 10
---> 36         x = self.hybrid(x)
     37         return T.cat((x, 1 - x), -1)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

<ipython-input-6-f66b87fe8d6c> in forward(self, input)
     42 
     43     def forward(self, input):
---> 44         return HybridFunction.apply(input, self.quantum_circuit, self.shift)

<ipython-input-6-f66b87fe8d6c> in forward(ctx, input, quantum_circuit, shift)
      8         ctx.quantum_circuit = quantum_circuit
      9 
---> 10         expectation_z = ctx.quantum_circuit.run(input[0].tolist())
     11         result = torch.tensor([expectation_z])
     12         ctx.save_for_backward(input, result)

<ipython-input-4-39b4287471c5> in run(self, thetas)
     30         result = job.result().get_counts()
     31 
---> 32         counts = np.array(list(result.values()))
     33         print('counts', counts)
     34         print('result.values', result.values())

AttributeError: 'list' object has no attribute 'values'

But the output of result after printing is:

RESULT [{'1': 49, '0': 51}, {'1': 58, '0': 42}, {'1': 48, '0': 52}, {'0': 53, '1': 47}, {'0': 51, '1': 49}, {'0': 51, '1': 49}, {'1': 58, '0': 42}, {'0': 47, '1': 53}, {'1': 54, '0': 46}, {'0': 47, '1': 53}, {'0': 41, '1': 59}, {'0': 52, '1': 48}, {'1': 47, '0': 53}, {'1': 52, '0': 48}, {'0': 52, '1': 48}, {'1': 50, '0': 50}, {'0': 41, '1': 59}, {'1': 54, '0': 46}, {'1': 55, '0': 45}, {'1': 44, '0': 56}, {'1': 61, '0': 39}, {'1': 53, '0': 47}, {'0': 40, '1': 60}, {'1': 59, '0': 41}, {'0': 60, '1': 40}, {'0': 56, '1': 44}, {'1': 46, '0': 54}, {'0': 46, '1': 54}, {'0': 53, '1': 47}, {'1': 52, '0': 48}, {'0': 49, '1': 51}, {'1': 55, '0': 45}, {'1': 51, '0': 49}, {'1': 48, '0': 52}, {'0': 53, '1': 47}, {'0': 56, '1': 44}, {'1': 53, '0': 47}, {'0': 51, '1': 49}, {'0': 45, '1': 55}, {'1': 47, '0': 53}, {'1': 55, '0': 45}]
$\endgroup$
1
  • 6
    $\begingroup$ I’m voting to close because exception stack trace is not a question at all, let alone one about quantum computing. $\endgroup$ Sep 16, 2022 at 18:07

1 Answer 1

1
$\begingroup$

Result.get_counts() method returns a dictionary if the Job contains only one circuit. If, however, your job contains multiple circuits, it will return a list of dictionaries.

To avoid this issue, you will need to either specify the circuit index:

# Counts of the first circuit:
result = job.result().get_counts(0)

or loop over the circuits:

for _dict in job.result().get_counts():
    print(_dict)
$\endgroup$
1
  • $\begingroup$ Thank you for the solution. But Later, it somehow throws the error"CircuitError: 'Expression cannot bind non-numeric values ({Parameter(theta): [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]})' Dont know where could be the possible error. $\endgroup$
    – user14924
    Sep 18, 2022 at 10:18

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