RuntimeError when setting custom Ca and Cb matrices in Wavefunction

RuntimeError when setting custom Ca and Cb matrices in Wavefunction

Hello Psi4 community,

I’m trying to initialize a Psi4 wavefunction with custom Ca and Cb matrices for post-SCF calculations without running an SCF cycle. My goal is to use Psi4’s post-SCF capabilities with orbital coefficients from an external program. Here’s a minimal example demonstrating the issue:

import psi4
import numpy as np

# Set up a simple hydrogen atom
molecule = psi4.geometry("""
0 2
H
symmetry c1
""")

# Set basic options
psi4.set_options({"basis": "sto-3g", "reference": "uhf"})

# Build initial wavefunction
wfn = psi4.core.Wavefunction.build(molecule, psi4.core.get_global_option("basis"))

# Get number of symmetry orbitals
nso = wfn.nso()

# Create mock Ca and Cb matrices
Ca_matrix = np.random.rand(nso, nso)
Cb_matrix = np.random.rand(nso, nso)

# Convert to psi4.core.Matrix objects
Ca = psi4.core.Matrix.from_array(Ca_matrix)
Cb = psi4.core.Matrix.from_array(Cb_matrix)

# Attempt to set Ca and Cb
wfn.Ca().copy(Ca)
wfn.Cb().copy(Cb)

When I run this code, I get the following error:

RuntimeError: 
Fatal Error: Wavefunction::Ca: Unable to obtain MO coefficients.
Error occurred in file: /scratch/psilocaluser/conda-builds/psi4-multiout_1670980379431/work/psi4/src/psi4/libmints/wavefunction.cc on line: 804
The most recent 5 function calls were:
psi::Wavefunction::Ca() const

My Psi4 version information:

psi4 --version
1.7
which conda python psi4
/opt/conda/condabin/conda /opt/conda/envs/p4env2/bin/python /opt/conda/envs/p4env2/bin/psi4

My questions are:

  1. Why can I not set the Ca and Cb matrices directly?
  2. Is there a way to initialize a wavefunction with custom orbital coefficients without running an SCF cycle?
  3. How can I create a fully customized wavefunction object that I can then use for post-SCF calculations?

I’ve searched the forum and found some related posts:

  1. Custom guess for Hartree-Fock
  2. Post-SCF with custom matrices
  3. CCSD with Arbitrary MO Coefficients

However, these don’t fully address my use case of initializing with dummy data without running an SCF cycle. The discussions in these threads suggest that additional steps might be needed to set up a wavefunction with custom matrices properly, but I’m still unclear on the exact procedure.

I appreciate your help, and I apologize if I’ve missed an existing answer to this question!

  1. wfn.Ca().copy(Ca) accesses the pointer with the wavefunction’s Ca Matrix and then calls that Matrix’s copy method. Becuase you haven’t run a SCF computation, the pointer is nullptr. The Ca method detects that the alpha coefficient matrix is nullptr and raises an error… which is a very good thing to do, because nullptr doesn’t have the copy method.
  2. No.
  3. I don’t know your research project, so I don’t know what your “fully customized wavefunction” is. I will have very different things to say if you’re using converged excited-state SCF solutions compared to if you want completely random molecular integrals and orbitals and you also want to be able to change your electron number.