使用 pytorch 和 qiskit 的多参数混合经典量子神经网络

问题描述 投票:0回答:1

我正在使用混合经典量子神经网络的代码,该代码可以在 qiskit 模拟器上运行。我正在点击这里的链接 [https://learn.qiskit.org/course/ch-applications/hybrid-quantum-classical-neural-networks-with-pytorch-and-qiskit]

此链接中的示例适用于量子电路中的单个可训练参数“theta”。但是,我想要量子电路中的多个参数。所以我修改了 QuantumCircuit 类如下。

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])

我保持其他一切不变。我收到以下错误:

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()

我认为发生错误是因为 Hybrid 类中的参数“input”仍然是浮点数。我所做的更改并没有影响它。我尝试手动将“输入”设置为 (1, 2) 维张量,但这也不起作用。如何修改类以包含更多可变参数?

machine-learning pytorch hybrid qiskit
1个回答
0
投票

我也遇到了这个问题。你可以通过修改你的qiskit和tera的版本来解决这个问题,因为教程的版本不是最新的。

© www.soinside.com 2019 - 2024. All rights reserved.