0

I am working with a code for hybrid classical-quantum neural network which can be run on qiskit simulator. I am following the link here [https://learn.qiskit.org/course/ch-applications/hybrid-quantum-classical-neural-networks-with-pytorch-and-qiskit]

The example in this link works with a single trainable parameter “theta” in the quantum circuit. However, I want multiple parameters in the quantum circuit. So I modified the QuantumCircuit class as below.

class QuantumCircuit:
    """ 
    This class provides a simple interface for interaction 
    with the quantum circuit 
    """
    
    def __init__(self, n_qubits, backend, shots):
        # --- Circuit definition ---
        self._circuit = qiskit.QuantumCircuit(n_qubits)
        
        all_qubits = [i for i in range(n_qubits)]
        self.theta = qiskit.circuit.ParameterVector('theta', 2)
        #self.phi = qiskit.circuit.Parameter('phi')
        
        self._circuit.h(all_qubits)
        self._circuit.barrier()
        self._circuit.ry(self.theta[0], all_qubits)
        self._circuit.ry(self.theta[1], all_qubits)
        
        self._circuit.measure_all()
        # ---------------------------

        self.backend = backend
        self.shots = shots
    
    def run(self, thetas):
        #t_qc = transpile(self._circuit,
                         #self.backend)
        #qobj = assemble(t_qc,
                        #shots=self.shots,
                        #parameter_binds = [{self.theta: theta} for theta in thetas])
        #job = self.backend.run(qobj)
        #job = self.backend.run(transpile([self._circuit.bind_parameters({self.theta[0]: theta[0], self.theta[1]: theta[1]}) for (theta, phi) in zip(thetas, phis)], backend=self.backend), shots=self.shots)
        job = self.backend.run(transpile([self._circuit.bind_parameters({self.theta: theta}) for theta in thetas], backend=self.backend), shots=self.shots)
        result = job.result().get_counts()
        
        counts = np.array(list(result.values()))
        states = np.array(list(result.keys())).astype(float)
        
        # Compute probabilities for each state
        probabilities = counts / self.shots
        # Get state expectation
        expectation = np.sum(states * probabilities)
        
        return np.array([expectation])

I kept everything else same. I am getting the following error:

Traceback (most recent call last):
File “/home/sreetamadas/Downloads/test.py”, line 198, in
output = model(data)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1501, in _call_impl
return forward_call(*args, **kwargs)
File “/home/sreetamadas/Downloads/test.py”, line 180, in forward
x = self.hybrid(x)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1501, in _call_impl
return forward_call(*args, **kwargs)
File “/home/sreetamadas/Downloads/test.py”, line 125, in forward
return HybridFunction.apply(input, self.quantum_circuit, self.shift)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/torch/autograd/function.py”, line 506, in apply
return super().apply(*args, **kwargs) # type: ignore[misc]
File “/home/sreetamadas/Downloads/test.py”, line 74, in forward
expectation_z = ctx.quantum_circuit.run(input[0].tolist())
File “/home/sreetamadas/Downloads/test.py”, line 50, in run
job = self.backend.run(transpile([self._circuit.bind_parameters({self.theta: theta}) for theta in thetas], backend=self.backend), shots=self.shots)
File “/home/sreetamadas/Downloads/test.py”, line 50, in
job = self.backend.run(transpile([self._circuit.bind_parameters({self.theta: theta}) for theta in thetas], backend=self.backend), shots=self.shots)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/qiskit/circuit/quantumcircuit.py”, line 2783, in bind_parameters
return self.assign_parameters(values)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/qiskit/circuit/quantumcircuit.py”, line 2725, in assign_parameters
unrolled_param_dict = self._unroll_param_dict(parameters)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/qiskit/circuit/quantumcircuit.py”, line 2797, in _unroll_param_dict
if not len(param) == len(value):
TypeError: object of type ‘float’ has no len()

I think the error is occurring because the parameter “input” in the class Hybrid is a still a float number. The changes I made did not affect it. I have tried to manually set “input” as a (1, 2) dimensional tensor, but that also did not work. How can I modify the classes to include more variable parameters?

desertnaut
  • 57,590
  • 26
  • 140
  • 166

1 Answers1

0

I also met this problem.You can solve this by modifying the version of your qiskit and tera,because the version of the tuorial is not the lastest.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34740756) – iantonuk Jul 30 '23 at 23:52