How do I retrieve computation results by ID?#

This tutorial introduces the basics of connecting to a QCArchive server and retrieving computation results.

When you retrieve results from QCArchive, they can be in the form of single records or a larger dataset.

A record represents a single quantum chemistry computation and contains the input details and the results. While some records represent simple computations (like a single point computation), others can encapsulate more complex workflows and multiple associated computations.

A dataset is a collection of similar records.

In this QuickStart, you’ll learn how to connect to the QCArchive Demo Server and retrieve records by their IDs. If you’d like to learn more about records, see the “How do I work with records?” tutorial.

import qcportal as ptl

Create a client object and connect to the demo server#

The PortalClient is how you interact with the server, including querying records and submitting computations.

The demo server allows for unauthenticated guest access, so no username/password is necessary to read from the server. However, you will need to log in to submit or modify computations.

# Guest access
client = ptl.PortalClient("https://qcademo.molssi.org")
---------------------------------------------------------------------------
PortalRequestError                        Traceback (most recent call last)
Cell In[2], line 2
      1 # Guest access
----> 2 client = ptl.PortalClient("https://qcademo.molssi.org")

File ~/work/QCFractal/QCFractal/qcportal/qcportal/client.py:150, in PortalClient.__init__(self, address, username, password, verify, show_motd, cache_dir, cache_max_size, memory_cache_key)
    116 def __init__(
    117     self,
    118     address: str,
   (...)
    126     memory_cache_key: Optional[str] = None,
    127 ) -> None:
    128     """
    129     Parameters
    130     ----------
   (...)
    147         Maximum size of the cache directory
    148     """
--> 150     PortalClientBase.__init__(self, address, username, password, verify, show_motd)
    151     self._logger = logging.getLogger("PortalClient")
    152     self.cache = PortalCache(address, cache_dir, cache_max_size)

File ~/work/QCFractal/QCFractal/qcportal/qcportal/client_base.py:156, in PortalClientBase.__init__(self, address, username, password, verify, show_motd)
    153     self._jwt_refresh_exp = None
    155 # Try to connect and pull the server info
--> 156 self.server_info = self.get_server_information()
    157 self.server_name = self.server_info["name"]
    158 self.api_limits = self.server_info["api_limits"]

File ~/work/QCFractal/QCFractal/qcportal/qcportal/client.py:190, in PortalClient.get_server_information(self)
    181 """Request general information about the server
    182 
    183 Returns
   (...)
    186     Server information.
    187 """
    189 # Request the info, and store here for later use
--> 190 return self.make_request("get", "api/v1/information", Dict[str, Any])

File ~/work/QCFractal/QCFractal/qcportal/qcportal/client_base.py:416, in PortalClientBase.make_request(self, method, endpoint, response_model, body_model, url_params_model, body, url_params, allow_retries)
    413 if isinstance(parsed_url_params, pydantic.BaseModel):
    414     parsed_url_params = parsed_url_params.dict()
--> 416 r = self._request(
    417     method, endpoint, body=serialized_body, url_params=parsed_url_params, allow_retries=allow_retries
    418 )
    419 d = deserialize(r.content, r.headers["Content-Type"])
    421 if response_model is None:

File ~/work/QCFractal/QCFractal/qcportal/qcportal/client_base.py:381, in PortalClientBase._request(self, method, endpoint, body, url_params, internal_retry, allow_retries)
    376     except:
    377         # If this error comes from, ie, the web server or something else, then
    378         # we have to use 'reason'
    379         details = {"msg": r.reason}
--> 381     raise PortalRequestError(f"Request failed: {details['msg']}", r.status_code, details)
    383 return r

PortalRequestError: Request failed: Not Found (HTTP status 404)

Connecting with username/password

If you have a username/password, you would include those in the client connection.

client = ptl.PortalClient("https://qcademo.molssi.org", username="YOUR_USERNAME", password="YOUR_PASSWORD")

⚠️Caution⚠️: Always handle credentials with care. Never commit sensitive information like usernames or passwords to public repositories.

Retrieving a Single Record by ID#

To retrieve a record, you can use the get_records method. You pass in the IDs of the records you would like to retrieve.

If a list of IDs is specified, then a list of records is returned. Otherwise, only a single record is returned.

record = client.get_records(1)
print(record)

From printing the record, we see that the record with ID 1 is a single point calculation (SinglePointRecord) and that this computation is “complete” (RecordStatusEnum.complete).

Viewing Record Information#

Records have lots of features, and what they have depends on the type of record - we will only cover a few here.

For the single point calculation we retrieved, we can see information about the molecule (molecule attribute), the method, basis, etc (specification), and more.

To see information about the molecule used in the calculation, use record.molecule.

# The molecule that we computed
print(record.molecule)

The information above tells us that the single point calculation was performed on a helium atom.

Molecule “hash”

The molecule hash is a unique identifier that takes atom identity, connectivity, coordinates, and fragmentation into account.

The record specification shows the program used for the calculation, as well as information about the method as basis.

# The specification (method, basis, etc)
print(record.specification)

The specification printed above tells us that this calculation was performed with the Psi4 software using the hf method and sto-3g basis set.

Retrieving Multiple Records#

The previous example showed retrieving just one record from QCArchive using the get_records method. However, more than one record at a time can be retrieved by passing a list of computation IDs to the method.

records = client.get_records([1, 2, 3])

print(f"Retrieved {len(records)} records.")

Using, the information presented earlier, we can see data about each computation.

for record in records:
    print(record, record.molecule)

Retrieving Records by Computation Type#

The QCArchive API also allows you to retrieve records based on the computation type. QCArchive supports different types of computations including single points, optimizations, torsion drives and more. In addition to using get_records, you can also use methods specific to the type of computation of interest. For example, to retrieve single point computations only, you can use the method get_singlepoints.

records = client.get_singlepoints([1,2])
print(records)