diff --git a/mindquantum/core/operators/hamiltonian.py b/mindquantum/core/operators/hamiltonian.py index 6de75ff86472f05219b063f6f29bd6210e4e75ad..b2204bef8808b06099c2cebb353614f82c269650 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/available_simulator.py b/mindquantum/simulator/available_simulator.py index 493a8fb85e855dc5e041a5967635e51428aeae18..b5551e0e412e4bc73b4588e150d22acec3036f92 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) diff --git a/mindquantum/simulator/mqsim.py b/mindquantum/simulator/mqsim.py index 8a7aae53451495074b419b1a1cb1a28c49dbb215..9cc13e18def5f61dec8f2bfc1ce6e7ae804cfb79 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,