{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Simple Usage\n", "\n", "The main functionality of the package is accessible through `nbed.nbed()`.\n", "\n", "There are three ways to provide arguments to the function:\n", "1. passing a path to a config `.json` file. \n", "2. passing named arguments directly.\n", "3. passing an NbedConfig model.\n", "\n", "Note that named arguments which are explicitly added will overwrite the config input from a file or model." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example Config file\n", "\n", "First lets see what's in the file before we pass it to the main function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "with open(\"test_config.json\") as f:\n", " config_from_file = json.load(f)\n", " \n", "config_from_file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from nbed import nbed\n", "\n", "result = nbed(config=\"test_config.json\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result.mu" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding arguments directly" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geometry= \"3\\n\\nO 0.0000 0.000 0.115\\nH 0.0000 0.754 -0.459\\nH 0.0000 -0.754 -0.459\"\n", "\n", "result = nbed(geometry=geometry, n_active_atoms=2, basis=\"sto-3g\", xc_functional=\"b3lyp\", projector=\"mu\", localization=\"spade\",convergence=1e-6, charge=0, spin=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Overwriting arguments\n", "Let's now overwrite some arguments, using the same config to embed some atoms of methane." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from nbed import nbed\n", "\n", "methane = \"\"\"5\n", "\n", "C\\t0.0\\t0.0\\t0.0\n", "H\\t0.5288\\t0.1610\\t0.9359\n", "H\\t0.2051\\t0.8240\\t-0.6786\n", "H\\t0.3345\\t-0.9314\\t-0.4496\n", "H\\t-1.0685\\t-0.0537\\t0.1921\n", "# \"\"\"\n", "\n", "result = nbed(config=\"test_config.json\", geometry=methane)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using an NbedConfig model.\n", "\n", "The final option is to directly pass the pydantic model that Nbed uses internally to validate data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from nbed.config import NbedConfig\n", "\n", "config = NbedConfig(geometry=geometry, n_active_atoms=2, basis=\"sto-3g\", xc_functional=\"b3lyp\", projector=\"mu\", localization=\"spade\",convergence=1e-6, charge=0, spin=0)\n", "result = nbed(config)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "config = NbedConfig(**config_from_file)\n", "result = nbed(config)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Command-line Interface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to run nbed from the command line, the `nbed` command will be installed with the package and allows you to input the path to a config file.\n", "\n", "This can be useful for running nbed over ssh." ] }, { "cell_type": "raw", "metadata": {}, "source": [ "nbed --config test_config.json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Results\n", "\n", "Results for the `mu` and `huzinaga` projectors are stored separately, let's take a look at what's included in there." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result.mu.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PySCf Object\n", "\n", "If you want to contnue to use PySCF methods on the embedded system, you can get the embedded PySCF object `result.mu[\"embedded_scf\"]` (usually a UKS object), together with a correction to the energy which represents the environment `result.mu[\"classical_energy\"]`. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result.mu[\"scf\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result.mu[\"classical_energy\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Second Quantised Hamiltonian\n", "\n", "The second quantised electronic structure hamiltonian will be the main thing you need if you're planning to run a quantum algorithm. You'll need to pair this with a [Fermion-Qubit encoding](https://ferrmion.readthedocs.io/) to create a qubit Hamiltonian that's well optimised to the device you intend to use.\n", "\n", "Nbed used the spin-orbit format, where the two spins of molecular orbital $i$: $(i_{\\uparrow}, i_{\\downarrow})$ map to indices $(2i, 2i+1)$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "constant, one_e_terms, two_e_terms = result.mu[\"second_quantised\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "constant, one_e_terms.shape, two_e_terms.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other information\n", "\n", "Most of the relevant information created or used in the embedding is accessible:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for k, v in result.mu.items():\n", " print(f\"{k}: {type(v)}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Nbed", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }