Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PicklingError: Can't pickle <function param_shift at 0x000001C66ECB9670>: it's not the same object as pennylane.gradients.parameter_shift.param_shift #275

Open
anhphuong-ngo opened this issue Feb 17, 2023 · 2 comments

Comments

@anhphuong-ngo
Copy link

anhphuong-ngo commented Feb 17, 2023

I am working on QNN model. My algorithm works fine with "default.qubit" device. However, it got issues when I try with 3rd-party device (e.g. qiskit IBMQ and qulacs). I appreciate any help and suggestions.

Same code

from torch.nn import Linear, CrossEntropyLoss, MSELoss
import pennylane as qml
from pennylane.qnode import QNode
torch.manual_seed(42)
from qiskit import IBMQ
provider = IBMQ.load_account()

*# Define and create QNN
qubit1 = 7
qubit2 = 7

*# dev1 = qml.device("default.qubit", wires=qubit1)
dev1 = qml.device('qiskit.ibmq', wires=qubit1, backend='ibmq_qasm_simulator', provider=provider)
def circuit1(inputs, weights):
    qml.AngleEmbedding(inputs, wires=range(qubit1))
    qml.BasicEntanglerLayers(weights, wires=range(qubit1))
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(qubit1)]
qnode1 = qml.QNode(circuit1, dev1, interface='torch')

*# dev2 = qml.device("default.qubit", wires=qubit2)
dev2 = qml.device('qiskit.ibmq', wires=qubit2, backend='ibmq_qasm_simulator', provider=provider)

def circuit2(inputs, weights):
    qml.AngleEmbedding(inputs, wires=range(qubit2))
    qml.BasicEntanglerLayers(weights, wires=range(qubit2))
    return [qml.expval(qml.PauliZ(wires=i)) for i in range(qubit2)]
qnode2 = qml.QNode(circuit2, dev2, interface='torch')

def create_qnn1(qubit1):
    n_layers = 7
    weight_shapes = {"weights": (n_layers, qubit1)}
    return qml.qnn.TorchLayer(qnode1, weight_shapes)

def create_qnn2(qubit2):
    n_layers = 7
    weight_shapes = {"weights": (n_layers, qubit2)}
    return qml.qnn.TorchLayer(qnode2, weight_shapes)

qnn1 = create_qnn1(7) #
qnn2 = create_qnn2(7)  # s_dim = 8

class Net(torch.nn.Module):
    def __init__(self, s_dim, a_dim):
        super().__init__()
        self.s_dim = s_dim
        self.a_dim = a_dim
        self.fc_pi1 = Linear(s_dim, 128)
        self.fc_pi2 = Linear(128, 7)
        self.qnn1 = qnn1
        self.fc_out11 = Linear(7, s_dim) #Output of quantum layer is only 1
        self.fc_out12 = Linear(s_dim,a_dim)
        self.fc_v1 = Linear(s_dim, 128)
        self.fc_v2 = Linear(128,2)
        self.qnn2 = qnn2
        self.out2 = Linear(7, 1)
        self.distribution = torch.distributions.Categorical

    def forward(self, x):
        pix = x
        vx  = x
        #Logits
        pix = self.fc_pi1(pix)
        pix = self.fc_pi2(pix)
        pix = self.qnn1(pix)
        pix = self.fc_out11(pix)
        pix = self.fc_out12(pix)
        logits = torch.tanh(pix)
        #Values
        vx = self.fc_v1(vx)
        vx = self.fc_v2(vx)
        vx = self.qnn2(vx)
        vx = self.out2(vx)
        values = torch.tanh(vx)
        
        return logits, values

Errors

IBMQ

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)

PicklingError: Can't pickle <function param_shift at 0x000001C66ECB9670>: it's not the same object as pennylane.gradients.parameter_shift.param_shift


WARNING: Ignoring invalid distribution -iskit-terra (c:\programdata\anaconda3\lib\site-packages)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input

Qulacs

  File "C:\Users\ango1\Desktop\Quantum Reinforcement Learning\RL_EV_Experiment04_Pennylane\code\HSA.py", line 327, in <module>
    [w.start() for w in workers]

  File "C:\Users\ango1\Desktop\Quantum Reinforcement Learning\RL_EV_Experiment04_Pennylane\code\HSA.py", line 327, in <listcomp>
    [w.start() for w in workers]

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)

  File "C:\ProgramData\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)

TypeError: cannot pickle 'qulacs_core.QuantumState' object


WARNING: Ignoring invalid distribution -iskit-terra (c:\programdata\anaconda3\lib\site-packages)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
@CatalinaAlbornoz
Copy link

Hi @anhphuong-ngo, could you please share the data you're using and the rest of your code? I'm not being able to replicate your error with the information provided.

If you can share a minimal but self-contained version of your code then we can help you better.

@edoaltamura
Copy link

Jumping on this conversation, we found a bug that might be related: qiskit-community/qiskit-machine-learning#607. The minor release that will go live later this week will fix the pickle compatibility within Qiskit machine learning, and there's a chance something similar was done in @Qiskit core too to fix similar bugs.

@anhphuong-ngo, have you tried running your code again with the latest plugin releases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants