Create a module of Fortran - Open-Quantum-Platform/openqp GitHub Wiki
What is a Module?
A module is a subroutine or function that can be called from any programming language, making it versatile and reusable across different environments. To achieve this level of interoperability, each module should have its own C API. While the following example uses Fortran, the same process can be applied to any other programming language.
Creating a Module Written in Fortran
Let's walk through the process of creating a module using a default example, get_state_overlap
.
1. Creating a Subroutine/Function in Fortran
First, define the subroutine or function within a Fortran module:
module nac_mod
implicit none
character(len=*), parameter :: module_name = "nac_mod"
public get_state_overlap
contains
subroutine get_basis_overlap(infos)
! Your subroutine implementation goes here
end subroutine get_basis_overlap
end module nac_mod
In this example, a public subroutine named get_basis_overlap
is defined, which takes infos
as argument. This subroutine is located in the source/oqp.modules
subdirectory. The infos
parameter contains all the computational data, which corresponds to mol
in the Python layer.
modules.F90
2. Defining the C Interface in Next, define the C interface for this subroutine:
subroutine state_overlap_C(c_handle) bind(C, name="get_state_overlap")
use nac_mod, only: get_state_overlap
type(oqp_handle_t) :: c_handle
type(information), pointer :: inf
inf => oqp_handle_get_info(c_handle)
call get_state_overlap(inf)
end subroutine state_overlap_C
This code snippet creates a subroutine named state_overlap_C
, which is bound to the C function get_state_overlap
. It uses the get_state_overlap
subroutine defined in the nac_mod
module. The subroutine takes c_handle
as argument, with c_handle
being a structure containing computation data.
include/oqp.h
3. Declaring the Subroutine in To expose this subroutine to other programming languages, declare it in the header file oqp.h
:
void get_state_overlap(struct oqp_handle_t *inf);
This declaration allows the subroutine to be accessed from C or any other language that can interface with C.
4. Calling the Subroutine from Python
Finally, you can call the subroutine from a Python script as follows:
oqp.get_state_overlap(mol)
In this Python call, mol
represents the molecular data structure that corresponds to the infos
argument in the Fortran subroutine. The get_state_overlap
function is now easily accessible from Python, allowing you to integrate it into your workflows seamlessly.