Carregar um Hamiltoniano do ficheiroLoading a Hamiltonian from file

Anteriormente, construímos hamiltonianos adicionando-lhe termos individuais.Previously, we constructed Hamiltonians by adding individual terms to it. Embora isto seja bom para pequenos exemplos, a química quântica em escala requer hamiltonianos com milhões ou biliões de termos.While this is fine for small examples, quantum chemistry at scale require Hamiltonians with millions or billions of terms. Tais hamiltonianos, gerados por pacotes de química como o NWChem, são demasiado grandes para serem importados à mão.Such Hamiltonians, generated by chemistry packages such as NWChem, are too large to import by hand. Nesta amostra, ilustramos como um FermionHamiltonian caso pode ser gerado automaticamente a partir de uma molécula representada pelo esquema de Broombridge.In this sample, we illustrate how a FermionHamiltonian instance may be automatically generated from a molecule represented by the Broombridge schema. Para referência, pode-se inspecionar a amostra fornecida LithiumHydrideGUI ou a RunSimulation amostra.For reference, one may inspect the provided LithiumHydrideGUI sample, or the RunSimulation sample. Está também disponível um suporte limitado para importação do formato consumido pela LIQUi/>.Limited support is also available for importing from the format consumed by LIQUi|>.

Consideremos o exemplo da molécula de azoto, que é fornecida na IntegralData/YAML pasta do repositório de amostras.Let us consider the example of the Nitrogen molecule, which is provided in the IntegralData/YAML folder of the samples repository. O método para carregar o Broombridge esquema é simples.The method for loading the Broombridge schema is straightforward.

// This is the name of the file we want to load
var filename = @"n2_1_00Re_sto3g.nw.out.yaml";
// This is the directory containing the file
var root = @"IntegralData\YAML";

// This deserializes a Broombridge file, given its filename.
var broombridge = Broombridge.Deserializers.DeserializeBroombridge($@"{root}\{filename}");

// Note that the deserializer returns a list of `ProblemDescription` instances 
// as the file might describe multiple Hamiltonians. In this example, there is 
// only one Hamiltonian. So we use `.First()`, which selects the first element of the list.
var problem = broombridge.ProblemDescriptions.First();

// This extracts the `OrbitalIntegralHamiltonian` from Broombridge format,
// then converts it to a fermion Hamiltonian, then to a Jordan-Wigner
// representation.
var orbitalIntegralHamiltonian = problem.OrbitalIntegralHamiltonian;
var fermionHamiltonian = orbitalIntegralHamiltonian.ToFermionHamiltonian(IndexConvention.UpDown);
var jordanWignerEncoding = fermionHamiltonian.ToPauliHamiltonian(Pauli.QubitEncoding.JordanWigner);

O esquema de Broombridge também contém sugestões para que o estado inicial seja preparado.The Broombridge schema also contains suggestions for the initial state to be prepared. As etiquetas, por "|G⟩" exemplo, para "|E1⟩" estes estados podem ser vistas inspecionando o ficheiro.The labels, e.g. "|G⟩" or "|E1⟩", for these states may be seen by inspecting the file. Para preparar estes estados iniciais, o qSharpData consumido pelos Q# algoritmos quânticos é obtido semelhante à secção anterior, mas com um parâmetro adicional selecionando o estado inicial pretendido.In order to prepare these initial states, the qSharpData consumed by the Q# quantum algorithms is obtained similar to the previous section, but with an additional parameter selecting the desired initial state. Por exemplo,For instance,

// The desired initial state, assuming that a description of it is present in the
// Broombridge schema.
var state = "|E1>";
var wavefunction = problem.Wavefunctions[state].ToIndexing(IndexConvention.UpDown);

// This creates the qSharpData consumable by the Q# chemistry library algorithms.
var qSharpHamiltonianData = jordanWignerEncoding.ToQSharpFormat();
var qSharpWavefunctionData = wavefunction.ToQSharpFormat();
var qSharpData = QSharpFormat.Convert.ToQSharpFormat(qSharpHamiltonianData, qSharpWavefunctionData);

Todos os passos acima podem ser abreviados utilizando métodos de conveniência fornecidos da seguinte forma.All the above steps may be abbreviated using provided convenience methods as follows.

// This is the name of the file we want to load
var filename = "...";

// This deserializes a Broombridge file, given its filename.
var broombridge = Broombridge.Deserializers.DeserializeBroombridge(filename);

// Note that the deserializer returns a list of `ProblemDescription` instances 
// as the file might describe multiple Hamiltonians. In this example, there is 
// only one Hamiltonian. So we use `.Single()`, which selects the only element of the list.
var problem = broombridge.ProblemDescriptions.Single();

// This is a data structure representing the Jordan-Wigner encoding 
// of the Hamiltonian that we may pass to a Q# algorithm.
// If no state is specified, the Hartree-Fock state is used by default.
var qSharpData = problem.ToQSharpFormat("|E1>");

Também podemos carregar um Hamiltonian do LIQUi.> formato, utilizando uma sintaxe muito semelhante.We may also load a Hamiltonian from the LIQUi|> format, using a very similar syntax.

// This is the name of the file we want to load
var filename = @"fe2s2_sto3g.dat"; // This is Ferrodoxin.
// This is the directory containing the file
var root = @"IntegralData\Liquid";

// Deserialize the LiQuiD format.
var problem = LiQuiD.Deserialize($@"{root}\{filename}").First();

// This is a data structure representing the Jordan-Wigner encoding 
// of the Hamiltonian that we may pass to a Q# algorithm.
var qSharpData = problem.ToQSharpFormat();

Seguindo este processo a partir de qualquer instância de Broombridge, ou qualquer passo intermédio, algoritmos quânticos como a estimativa da fase quântica podem ser executados no problema da estrutura electrónica especificada.By following this process from any instance of Broombridge, or any intermediate step, quantum algorithms such as quantum phase estimation may be run on the specified electronic structure problem.