# MindSpore使用run\_pyscf跑量子化学时报错Invalid cross-device link

**1. 系统环境**

硬件环境\(Ascend/GPU/CPU\): Ascend/GPU/CPU

软件环境: MindSpore 版本: 2.0.0

执行模式（静态图/动态图）： 不限

Python 版本: 3.9

操作系统平台: linux

**2. 报错信息**

2.1 问题描述

MindSpore使用run\_pyscf跑量子化学时报错Invalid cross-device link。

2.2 报错信息

```
Traceback (most recent call last): 
  File "/usr/lib/python3.7/shutil.py", line 566, in move 
    os.rename(src, real_dst) 
OSError: [Errno 18] Invalid cross-device link: 'ddce6e68-a287-4612-91c1-775b3d9852b5.hdf5' -> '/usr/local/lib/python3.7/dist-packages/openfermion/testing/data/H1-Li1_sto3g_singlet.hdf5' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
  File "fci.py", line 31, in <module> 
    run_fci=1 
  File "/usr/local/lib/python3.7/dist-packages/openfermionpyscf/_run_pyscf.py", line 205, in run_pyscf 
    pyscf_molecular_data.save() 
  File "/usr/local/lib/python3.7/dist-packages/openfermion/chem/molecular_data.py", line 846, in save 
    shutil.move("{}.hdf5".format(tmp_name), "{}.hdf5".format(self.filename)) 
  File "/usr/lib/python3.7/shutil.py", line 580, in move 
    copy_function(src, real_dst) 
  File "/usr/lib/python3.7/shutil.py", line 266, in copy2 
    copyfile(src, dst, follow_symlinks=follow_symlinks) 
  File "/usr/lib/python3.7/shutil.py", line 121, in copyfile 
    with open(dst, 'wb') as fdst: 
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.7/dist-packages/openfermion/testing/data/H1-Li1_sto3g_singlet.hdf5' 
```

2.3 脚本信息

```
from openfermion.chem import MolecularData
from openfermionpyscf import run_pyscf
from mindquantum.core.gates import X
from mindquantum.core.circuit import Circuit
from mindquantum.core.operators import Hamiltonian
from mindquantum.simulator import Simulator
from mindquantum.algorithm.nisq import generate_uccsd
import mindspore as ms

ms.set_context(mode=ms.PYNATIVE_MODE, device_target="CPU")
dist = 1.5
geometry = [
    ["Li", [0.0, 0.0, 0.0 * dist]],
    ["H", [0.0, 0.0, 1.0 * dist]],
]
basis = "sto3g"
spin = 0
print("Geometry: \n", geometry)
molecule_of = MolecularData(
    geometry,
    basis,
    multiplicity=2 * spin + 1
)

molecule_of = run_pyscf(
    molecule_of,
    run_scf=1,
    run_ccsd=1,
    run_fci=1
)

print("Hartree-Fock energy: %20.16f Ha" % (molecule_of.hf_energy))
print("CCSD energy: %20.16f Ha" % (molecule_of.ccsd_energy))
print("FCI energy: %20.16f Ha" % (molecule_of.fci_energy))
```

**3. 根因分析**

这是由于分子文件默认存储到了系统目录，权限不足导致的。

**4. 解决方案**

将data\_directory='./'传入到MolecularData中。

```
from openfermion.chem import MolecularData
from openfermionpyscf import run_pyscf
from mindquantum.core.gates import X
from mindquantum.core.circuit import Circuit
from mindquantum.core.operators import Hamiltonian
from mindquantum.simulator import Simulator
from mindquantum.algorithm.nisq import generate_uccsd
import mindspore as ms

ms.set_context(mode=ms.PYNATIVE_MODE, device_target="CPU")
dist = 1.5
geometry = [
    ["Li", [0.0, 0.0, 0.0 * dist]],
    ["H", [0.0, 0.0, 1.0 * dist]],
]
basis = "sto3g"
spin = 0
print("Geometry: \n", geometry)
molecule_of = MolecularData(geometry, basis, multiplicity=2 * spin + 1, data_directory='./')

molecule_of = run_pyscf(
    molecule_of,
    run_scf=1,
    run_ccsd=1,
    run_fci=1
)

print("Hartree-Fock energy: %20.16f Ha" % (molecule_of.hf_energy))
print("CCSD energy: %20.16f Ha" % (molecule_of.ccsd_energy))
print("FCI energy: %20.16f Ha" % (molecule_of.fci_energy))
```

