From fd8e2a1c7d303007ce296b7c3b9c2473a8e83897 Mon Sep 17 00:00:00 2001 From: EBB2675 Date: Mon, 10 Feb 2025 11:31:46 +0100 Subject: [PATCH] essential quantities for Qobj static attributes --- src/nomad_parser_qutip/parsers/__init__.py | 21 ++--- src/nomad_parser_qutip/parsers/parser.py | 11 ++- .../schema_packages/schema_package.py | 92 +++++++++++++++++-- tests/data/example.out | 1 + 4 files changed, 100 insertions(+), 25 deletions(-) diff --git a/src/nomad_parser_qutip/parsers/__init__.py b/src/nomad_parser_qutip/parsers/__init__.py index e482460..b03f11c 100644 --- a/src/nomad_parser_qutip/parsers/__init__.py +++ b/src/nomad_parser_qutip/parsers/__init__.py @@ -2,17 +2,14 @@ from pydantic import Field -class NewParserEntryPoint(ParserEntryPoint): - parameter: int = Field(0, description='Custom configuration parameter') - +class QutipEntryPoint(ParserEntryPoint): def load(self): - from nomad_parser_qutip.parsers.parser import NewParser - - return NewParser(**self.dict()) - - -parser_entry_point = NewParserEntryPoint( - name='NewParser', - description='New parser entry point configuration.', - mainfile_name_re='.*\.newmainfilename', + from nomad_parser_qutip.parsers.parser import QutipParser + return QutipParser(**self.dict()) + +parser_entry_point = QutipEntryPoint( + name='QuTiP Parser', + description='Parser for QuTiP outputs in text format.', + mainfile_name_re='.*\.out.*', + mainfile_contents_re=r'EBB2675 Version [\d\.]*', # to be fixed later on with real vals ) diff --git a/src/nomad_parser_qutip/parsers/parser.py b/src/nomad_parser_qutip/parsers/parser.py index d07de76..120b271 100644 --- a/src/nomad_parser_qutip/parsers/parser.py +++ b/src/nomad_parser_qutip/parsers/parser.py @@ -11,15 +11,17 @@ ) from nomad.config import config -from nomad.datamodel.metainfo.workflow import Workflow +from nomad.units import ureg from nomad.parsing.parser import MatchingParser +from nomad_simulations.schema_packages.general import Program, Simulation + configuration = config.get_plugin_entry_point( 'nomad_parser_qutip.parsers:parser_entry_point' ) -class NewParser(MatchingParser): +class QutipParser(MatchingParser): def parse( self, mainfile: str, @@ -27,6 +29,7 @@ def parse( logger: 'BoundLogger', child_archives: dict[str, 'EntryArchive'] = None, ) -> None: - logger.info('NewParser.parse', parameter=configuration.parameter) + simulation = Simulation() + simulation.program = Program(name='QuTiP') + archive.data = simulation - archive.workflow2 = Workflow(name='test') diff --git a/src/nomad_parser_qutip/schema_packages/schema_package.py b/src/nomad_parser_qutip/schema_packages/schema_package.py index 4223824..122ea99 100644 --- a/src/nomad_parser_qutip/schema_packages/schema_package.py +++ b/src/nomad_parser_qutip/schema_packages/schema_package.py @@ -13,7 +13,13 @@ from nomad.config import config from nomad.datamodel.data import Schema from nomad.datamodel.metainfo.annotations import ELNAnnotation, ELNComponentEnum -from nomad.metainfo import Quantity, SchemaPackage +from nomad.metainfo import (Quantity, + SchemaPackage, + SubSection, + MEnum, + Section) +from nomad.datamodel.data import ArchiveSection + configuration = config.get_plugin_entry_point( 'nomad_parser_qutip.schema_packages:schema_package_entry_point' @@ -22,17 +28,85 @@ m_package = SchemaPackage() -class NewSchemaPackage(Schema): - name = Quantity( - type=str, a_eln=ELNAnnotation(component=ELNComponentEnum.StringEditQuantity) +class Qobj(ArchiveSection): + """ + A section to semantically represent a QuTiP Qobj. + + The main quantities are: + dims : The Hilbert space dimensions as a nested list (e.g. [[2], [1]] for a ket). + shape : The shape of the underlying data matrix (e.g. [2, 1] for a column vector). + type : A string (enumerated) to label the Qobj (e.g. 'ket', 'bra', 'oper', 'super'). + dtype : The data type indicator (for example, 'Dense' or 'csr') showing the + numerical storage format. + isherm : A Boolean flag indicating whether an operator is Hermitian. + (For state vectors, this property is typically not meaningful.) + data : The numerical data stored as a 2D array (matrix) containing the Qobj values. + """ + + dims = Quantity( + type=list, + shape=['*', '*'], + description=( + """Hilbert space dimensions of the quantum object. For example, a ket + state in a 2-dimensional space may have dims=[[2], [1]].""" + ), ) - message = Quantity(type=str) - def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None: - super().normalize(archive, logger) + shape = Quantity( + type=list, + shape=['*'], + description=( + """Shape of the underlying data matrix. For example, a state vector (ket) + might have shape=[2, 1].""" + ), + ) + + type = Quantity( + type=MEnum('ket', 'bra', 'oper', 'super'), + description=( + """Type of the quantum object. Examples include 'ket' for state vectors, + 'bra' for dual vectors, 'oper' for operators, and 'super' for superoperators.""" + ), + ) - logger.info('NewSchema.normalize', parameter=configuration.parameter) - self.message = f'Hello {self.name}!' + dtype = Quantity( + type=str, + description=( + """Numerical storage format of the Qobj's data. For example, 'Dense' for a + full matrix or 'csr' for a compressed sparse row representation.""" + ), + ) + + isherm = Quantity( + type=bool, + description=( + """Flag indicating whether the operator is Hermitian.""" + ), + ) + data = Quantity( + type=np.ndarray, + shape=['*', '*'], + description=( + """The numerical matrix data of the quantum object. + It is stored as a 2D array.""" + ), + ) + + def normalize(self, archive, logger) -> None: + """ + Normalization method to check consistency of the Qobj. + + For instance, one could verify that the provided `shape` matches the shape of `data` + or that the product of the dimensions implied by `dims` is consistent with `data.shape`. + """ + super().normalize(archive, logger) + # this is just a placeholder, will take care of it later. + if self.data is not None: + data_shape = list(self.data.shape) + if self.shape != data_shape: + logger.warning( + f"Inconsistent shape: quantity 'shape' is {self.shape} but data.shape is {data_shape}." + ) m_package.__init_metainfo__() diff --git a/tests/data/example.out b/tests/data/example.out index e69de29..da9e621 100644 --- a/tests/data/example.out +++ b/tests/data/example.out @@ -0,0 +1 @@ +EBB2675 Version 0.0.1