Create a TagArray - Open-Quantum-Platform/openqp GitHub Wiki

What is a Tagarray?

A tagarray is an array of any type associated with a tag, allowing it to be accessed from any programming language using that tag. This functionality is particularly useful for passing data between different computer languages. The source code for tagarray is located in the external/libtagarray subdirectory. The general tagarrays utilized by Open Quantum Platform (OQP) are defined in source/tagarray_driver.F90.

Creating a Tagarray in Python and Passing It to Fortran

Before proceeding, ensure that oqp.Fortran_example is available by following the process described in Creating a New Module in Fortran.

1. Creating a Tagarray in Python

In this example, we create a tagarray in a Python script using NumPy. The tag "OQP::Python_matrix" is assigned to the matrix, and then the Fortran subroutine oqp.Fortran_example is called.

import numpy as np

# Create a tagarray in Python and assign it to the OQP::Python_matrix tag
mol.data["OQP::Python_matrix"] = np.reshape([1, 2.0, 3, 4.0, 5, 6], (2, 3)) 

# Call the Fortran subroutine
oqp.Fortran_example(mol)

2. Accessing the Tagarray in Fortran

The following Fortran code demonstrates how to utilize the "OQP::Python_matrix" tagarray created in the Python script.

module example_mod
  implicit none
  character(len=*), parameter :: module_name = "example_mod"
  public Fortran_example

contains

  subroutine Fortran_example(infos, alog)
    use oqp_tagarray_driver, only: data_has_tags, tagarray_get_data, ta_type_real64
    implicit none
    character(len=*), parameter :: subroutine_name = "Fortran_example"

    ! 1) Define the tag for the tagarray
    character(len=*), parameter :: tags_general(*) = (/ "OQP::Python_matrix" /)

    ! 2) Define a pointer to the Python_matrix tagarray
    real(kind=dp), contiguous, pointer :: Python_matrix(:,:)

    ! 3) Load the data that was previously allocated in the Python script
    call data_has_tags(infos%dat, tags_general, module_name, subroutine_name, with_abort)
    call tagarray_get_data(infos%dat, "OQP::Python_matrix", Python_matrix)

    ! Example usage: Print the matrix
    do i = 1, ubound(Python_matrix, 1)
      write(*,*) i, (Python_matrix(i,j), j = 1, ubound(Python_matrix, 2))
    end do

  end subroutine Fortran_example

end module example_mod

Creating a Tagarray in Fortran and Passing It to Python

Again, ensure that oqp.Fortran_example is available as described in Creating a New Module in Fortran.

1. Creating a Tagarray in Fortran

The following Fortran code creates a tagarray named "Fortran_matrix" and assigns it data.

module example_mod
  implicit none
  character(len=*), parameter :: module_name = "example_mod"
  public Fortran_example

contains

  subroutine Fortran_example(infos, alog)
    use oqp_tagarray_driver, only: data_has_tags, tagarray_get_data, ta_type_real64
    implicit none
    character(len=*), parameter :: subroutine_name = "Fortran_example"
    character(len=*), parameter :: tags_alloc(*) = (/ "OQP::Fortran_matrix" /)

    ! Define a pointer to the Fortran_matrix tagarray
    real(kind=dp), contiguous, pointer :: Fortran_matrix(:,:)

    ! a) Define the size and allocate memory
    call infos%dat%remove_records(tags_alloc)
    call infos%dat%reserve_data("OQP::Fortran_matrix", ta_type_real64, &
          2*3, (/ 2, 3 /), comment="An example of allocated data in Fortran")
    call data_has_tags(infos%dat, tags_alloc, module_name, subroutine_name, with_abort)
    call tagarray_get_data(infos%dat, "OQP::Fortran_matrix", Fortran_matrix)
    if (ok /= 0) call show_message('Cannot allocate memory', with_abort)

    ! b) Assign data to the Fortran_matrix
    Fortran_matrix = reshape([0.1d0, 0.2d0, 0.3d0, 0.4d0, 0.5d0, 0.6d0], [2, 3])

    write(*, "(/16X,43('=')/16X,a/16X,43('='))") &
        'This is an example of tagarray defined in Fortran'

  end subroutine Fortran_example

end module example_mod

2. Accessing the Tagarray in Python

The tagarray created in Fortran can now be accessed in a Python script using the following code:

# Access the Fortran_matrix tagarray created in Fortran
result = mol.data["OQP::Fortran_matrix"]

# Example usage: Print the matrix
print(result)

This approach allows seamless data sharing between Fortran and Python, leveraging the tagarray for efficient data management and access across different languages.

Going back to Code Examples

⚠️ **GitHub.com Fallback** ⚠️