From 4eb9bcf2947ea4fb1ae7cea3e5c8bcbfe60dda0d Mon Sep 17 00:00:00 2001 From: dsdsdshe Date: Tue, 9 Apr 2024 22:52:15 +0800 Subject: [PATCH 1/2] adapt pickle --- mindquantum/core/operators/hamiltonian.py | 19 +++++++++++++++++++ mindquantum/simulator/mqsim.py | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/mindquantum/core/operators/hamiltonian.py b/mindquantum/core/operators/hamiltonian.py index 6de75ff86..b2204bef8 100644 --- a/mindquantum/core/operators/hamiltonian.py +++ b/mindquantum/core/operators/hamiltonian.py @@ -105,6 +105,25 @@ class Hamiltonian: return self.sparse_mat.__str__() return self.hamiltonian.__repr__() + def __getstate__(self): + """Create a dictionary that will be pickled.""" + state = self.__dict__.copy() + # Remove the unpicklable entries. + if self.ham_cpp: + state['ham_cpp'] = True + if self.herm_ham_cpp: + state['herm_ham_cpp'] = True + return state + + def __setstate__(self, state): + """Restore instance state from the unpickled state.""" + self.__dict__.update(state) + # Add the missing 'ham_cpp' and 'herm_ham_cpp' entries + if self.ham_cpp: + self.ham_cpp = self.get_cpp_obj(hermitian=False) + if self.herm_ham_cpp: + self.herm_ham_cpp = self.get_cpp_obj(hermitian=True) + def sparse(self, n_qubits=1): """ Calculate the sparse matrix of this hamiltonian in pqc operator. diff --git a/mindquantum/simulator/mqsim.py b/mindquantum/simulator/mqsim.py index 8a7aae534..9cc13e18d 100644 --- a/mindquantum/simulator/mqsim.py +++ b/mindquantum/simulator/mqsim.py @@ -52,11 +52,13 @@ class MQSim(BackendBase): def __init__(self, name: str, n_qubits: int, seed=None, dtype=complex128, internal=False): """Initialize a mindquantum backend.""" super().__init__(name, n_qubits, seed, dtype) + self.internal = internal if internal: self.sim = name else: if dtype is None: dtype = complex128 + self.arithmetic_type = dtype _check_mq_type(dtype) self.sim = getattr(SUPPORTED_SIMULATOR.c_module(name, dtype), name)(n_qubits, seed) @@ -77,6 +79,22 @@ class MQSim(BackendBase): """Return a string representation of the object.""" return self.__str__() + def __getstate__(self): + state = self.__dict__.copy() + # Remove the unpicklable entries. + del state['sim'] + return state + + def __setstate__(self, state): + self.__dict__.update(state) + # Restore the sim object + if self.internal: + self.sim = self.name + else: + self.sim = getattr(SUPPORTED_SIMULATOR.c_module(self.name, self.arithmetic_type), self.name)( + self.n_qubits, self.seed + ) + def apply_circuit( self, circuit: Circuit, -- Gitee From 85169a1eb7df81729e42d5b8fa56a04f268f680d Mon Sep 17 00:00:00 2001 From: dsdsdshe Date: Wed, 10 Apr 2024 16:51:54 +0800 Subject: [PATCH 2/2] modify warning to be clear --- mindquantum/simulator/available_simulator.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mindquantum/simulator/available_simulator.py b/mindquantum/simulator/available_simulator.py index 493a8fb85..b5551e0e4 100644 --- a/mindquantum/simulator/available_simulator.py +++ b/mindquantum/simulator/available_simulator.py @@ -28,7 +28,11 @@ try: _mq_vector_gpu.double.mqvector_gpu(1).apply_gate(mqbackend.gate.HGate([0])) MQVECTOR_GPU_SUPPORTED = True except ImportError as err: - warnings.warn(f"Unable import mqvector gpu backend due to: {err}", stacklevel=2) + warnings.warn( + f"Unable import mqvector gpu backend. This could be due to your environment " + "not satisfying the requirements for mqvector_gpu, or the _mq_vector_gpu library may be missing.", + stacklevel=2, + ) MQVECTOR_GPU_SUPPORTED = False except RuntimeError as err: warnings.warn(f"Disable mqvector gpu backend due to: {err}", stacklevel=2) -- Gitee