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

AP_M: <p>When I run the code, getting the error as:</p>
<pre><code class="lang-auto">AttributeError: 'list' object has no attribute 'values'
</code></pre>
<pre><code class="lang-auto">AttributeError                            Traceback (most recent call last)
&lt;ipython-input-37-66115a52c5d6&gt; in &lt;module&gt;
     23     optimizer.zero_grad()
     24 
---&gt; 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:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

&lt;ipython-input-34-4a7275625fa6&gt; in forward(self, x)
     13 
     14     def forward(self,x):
---&gt; 15         h = self.f(x)
     16         return self.g(h)
     17 

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

&lt;ipython-input-32-ae2fe8e8ca2d&gt; in forward(self, x)
     34         x = self.fc(x[:, -1, :])
     35         # out.size() --&gt; 100, 10
---&gt; 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:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

&lt;ipython-input-6-f66b87fe8d6c&gt; in forward(self, input)
     42 
     43     def forward(self, input):
---&gt; 44         return HybridFunction.apply(input, self.quantum_circuit, self.shift)

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

&lt;ipython-input-4-34122f68ae18&gt; in run(self, thetas)
     30         result = job.result().get_counts()
     31 
---&gt; 32         counts = np.array(list(result.values()))
     33         states = np.array(list(result.keys())).astype(float)
     34 

AttributeError: 'list' object has no attribute 'values'
</code></pre>

ptrblck: <p>The error seems to be raised in <code>ctx.quantum_circuit.run</code> which seems to be another library.<br>
Since <code>result.values()</code> and <code>result.keys()</code> are expected, I would assume this method expects <code>results</code> to be a <code>dict</code> and not a <code>list</code>.</p>

AP_M: <p>The output of result is below which already has key value pairs.</p>
<pre><code class="lang-auto">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}]
</code></pre>

ptrblck: <p>That’s not entirely true, since your output shows a <code>list</code> containing <code>dict</code>s, which will still fail if you call <code>.values()</code> or <code>keys()</code> on it.</p>

AP_M: <p>According to this error, how should I make the changes in my code?</p>

ptrblck: <p>Make sure to pass a <code>dict</code> to this method, not a <code>list</code>.</p>