Commit d9b94ffa authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding hamiltonian test to python api


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent a5bd3935
Pipeline #94748 failed with stage
in 3 minutes and 53 seconds
import sys
from pathlib import Path
sys.path.insert(1, str(Path.home()) + '/.exatn')
import exatn, numpy as np
# Declare MPS tensors:
exatn.createTensor('Q0', [2,2], 1e-2)
exatn.createTensor('Q1', [2,2,4], 1e-2)
exatn.createTensor('Q2', [4,2,2], 1e-2)
exatn.createTensor('Q3', [2,2], 1e-2)
# Declare Hamiltonian Tensors
exatn.createTensor('H01', [2,2,2,2], 1e-2)
exatn.createTensor('H12', [2,2,2,2], 1e-2)
exatn.createTensor('H23', [2,2,2,2], 1e-2)
exatn.createTensor('Z0', [2,2,2,2], 1e-2)
# Get them as exatn.Tensor
q0 = exatn.getTensor('Q0')
q1 = exatn.getTensor('Q1')
q2 = exatn.getTensor('Q2')
q3 = exatn.getTensor('Q3')
h01 = exatn.getTensor('H01')
h12 = exatn.getTensor('H12')
h23 = exatn.getTensor('H23')
z0 = exatn.getTensor('Z0')
# Declare the Hamiltonian Operator
ham = exatn.TensorOperator('Hamiltonian')
ham.appendComponent(h01, [[0,0],[1,1]], [[0,2],[1,3]], 1.0)
ham.appendComponent(h12, [[1,0],[2,1]], [[1,2],[2,3]], 1.0)
ham.appendComponent(h23, [[2,0],[3,1]], [[2,2],[3,3]], 1.0)
# Declare the ket MPS tensor network:
# Q0----Q1----Q2----Q3
# | | | |
mps_ket = exatn.TensorNetwork('MPS', 'Z0(i0,i1,i2,i3)+=Q0(i0,j0)*Q1(j0,i1,j1)*Q2(j1,i2,j2)*Q3(j2,i3)', {'Z0':z0, 'Q0':q0, 'Q1':q1, 'Q2':q2, 'Q3':q3})
# Declare the ket tensor network expansion:
# Q0----Q1----Q2----Q3
# | | | |
ket = exatn.TensorExpansion()
ket.appendComponent(mps_ket, 1.0)
ket.rename('MPSket')
# Declare the bra tensor network expansion (conjugated ket):
# | | | |
# Q0----Q1----Q2----Q3
bra = exatn.TensorExpansion(ket)
bra.conjugate()
bra.rename('MPSbra')
# Declare the operator times ket product tensor expansion:
# Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3
# | | | | | | | | | | | |
# ==H01== | | + | ==H12== | + | | ==H23==
# | | | | | | | | | | | |
ham_ket = exatn.TensorExpansion(ket, ham)
ham_ket.rename('HamMPSket')
# Declare the full closed product tensor expansion (scalar):
# Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3
# | | | | | | | | | | | |
# ==H01== | | + | ==H12== | + | | ==H23== => AC0()
# | | | | | | | | | | | |
# Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3
closed_prod = exatn.TensorExpansion(ham_ket, bra)
closed_prod.rename('MPSbraHamMPSket')
closed_prod.printIt()
# Declare the derivative tensor expansion with respect to tensor Q1+:
# Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3 Q0----Q1----Q2----Q3
# | | | | | | | | | | | |
# ==H01== | | + | ==H12== | + | | ==H23==
# | | | | | | | | | | | |
# Q0-- --Q2----Q3 Q0-- --Q2----Q3 Q0-- --Q2----Q3
deriv_q1 = exatn.TensorExpansion(closed_prod, 'Q1', True)
deriv_q1.rename('DerivativeQ1')
# Create the Accumulator tensor for the closed tensor expansion:
exatn.createTensor('AC0')
accumulator0 = exatn.getTensor('AC0')
exatn.createTensor('AC1',[2,2,4], 0.0)
accumulator1 = exatn.getTensor('AC1')
# Evaluate the expectation value:
exatn.evaluate(closed_prod, accumulator0)
# Evaluate the derivative of the expectation value w.r.t. tensor Q1:
exatn.evaluate(deriv_q1, accumulator1)
# Print the expectation values
print(exatn.getLocalTensor('AC0'))
[print(exatn.getLocalTensor(c.network.getTensor(0).getName())) for c in closed_prod]
\ No newline at end of file
......@@ -5,7 +5,10 @@
#include "tensor_basic.hpp"
#include "exatn_numerics.hpp"
#include "tensor_expansion.hpp"
#include "tensor_network.hpp"
#include "tensor_operation.hpp"
#include "tensor_operator.hpp"
namespace py = pybind11;
using namespace exatn;
......@@ -22,12 +25,48 @@ void create_exatn_py_module(py::module &m) {
.def("getName", &exatn::numerics::Tensor::getName, "")
.def("getRank", &exatn::numerics::Tensor::getRank, "");
py::class_<TensorExpansion::ExpansionComponent, std::shared_ptr<TensorExpansion::ExpansionComponent>>(m, "ExpansionComponent", "")
.def_readonly("network", &TensorExpansion::ExpansionComponent::network_,"");
py::class_<exatn::numerics::TensorOperator,
std::shared_ptr<exatn::numerics::TensorOperator>>(
m, "TensorOperator", "")
.def(py::init<std::string>())
.def(
"appendComponent",
[](TensorOperator &_operator, std::shared_ptr<Tensor> tensor,
const std::vector<std::pair<unsigned int, unsigned int>>
&ket_pairing,
const std::vector<std::pair<unsigned int, unsigned int>>
&bra_pairing,
const std::complex<double> coefficient) {
return _operator.appendComponent(tensor, ket_pairing, bra_pairing,
coefficient);
},
"");
py::class_<exatn::numerics::TensorExpansion,
std::shared_ptr<exatn::numerics::TensorExpansion>>(m, "TensorExpansion", "")
.def(py::init<>())
.def(py::init<const TensorExpansion&>())
.def(py::init<const TensorExpansion&, const TensorOperator&>())
.def(py::init<const TensorExpansion&, const TensorExpansion&>())
.def(py::init<const TensorExpansion&, const TensorExpansion&, const TensorOperator&>())
.def(py::init<const TensorExpansion&, const std::string&, bool>())
.def("appendComponent", &TensorExpansion::appendComponent, "")
.def("conjugate", &TensorExpansion::conjugate, "")
.def("printIt", &TensorExpansion::printIt, "")
.def("rename", &TensorExpansion::rename, "")
.def("__iter__", [](const TensorExpansion &s) { return py::make_iterator(s.cbegin(), s.cend()); },
py::keep_alive<0, 1>());
py::class_<exatn::numerics::TensorNetwork,
std::shared_ptr<exatn::numerics::TensorNetwork>>(
m, "TensorNetwork", "")
.def(py::init<>())
.def(py::init<const std::string>())
.def(py::init<const TensorNetwork &>())
.def(py::init<const std::string &, const std::string &,
const std::map<std::string, std::shared_ptr<Tensor>>>())
.def("printIt", &exatn::numerics::TensorNetwork::printIt, "")
.def("rename", &exatn::numerics::TensorNetwork::rename, "")
.def("getName", &exatn::numerics::TensorNetwork::getName, "")
......@@ -200,6 +239,19 @@ void create_exatn_py_module(py::module &m) {
return success;
},
"");
m.def(
"createTensor",
[](const std::string &name, std::vector<std::size_t> dims,
std::complex<double> &init_value) {
auto success =
exatn::createTensor(name, exatn::TensorElementType::COMPLEX64,
exatn::numerics::TensorShape(dims));
if (success) {
return exatn::initTensorSync(name, init_value);
}
return success;
},
"");
m.def(
"createTensor",
[](const std::string &name) {
......@@ -228,6 +280,8 @@ void create_exatn_py_module(py::module &m) {
m.def(
"evaluate", [](TensorNetwork &network) { return evaluateSync(network); },
"");
m.def("evaluate", [](TensorExpansion& exp, std::shared_ptr<Tensor> accum){return exatn::evaluateSync(exp,accum);});
m.def("getTensor", &exatn::getTensor, "");
m.def("print", &printTensorDataNoNumServer, "");
m.def("transformTensor", &generalTransformWithDataNoNumServer, "");
m.def("evaluateTensorNetwork", &evaluateTensorNetwork, "");
......
......@@ -188,9 +188,80 @@ assert(exatn.evaluate(inverse))
print(exatn.getLocalTensor(inverse.getTensor(0).getName()))
[exatn.destroyTensor('Q'+str(i)) for i in range(nQubits)]
exatn.destroyTensor('H')
exatn.destroyTensor('Z0')
)""");
py::print("[ Done ]");
py::print("\n[ Test Hamiltonian ]");
py::exec(
R"""(
import sys
from pathlib import Path
sys.path.insert(1, str(Path.home()) + '/.exatn')
import exatn, numpy as np
exatn.createTensor('Q0', [2,2], 1e-2)
exatn.createTensor('Q1', [2,2,4], 1e-2)
exatn.createTensor('Q2', [4,2,2], 1e-2)
exatn.createTensor('Q3', [2,2], 1e-2)
exatn.createTensor('H01', [2,2,2,2], 1e-2)
exatn.createTensor('H12', [2,2,2,2], 1e-2)
exatn.createTensor('H23', [2,2,2,2], 1e-2)
exatn.createTensor('Z0', [2,2,2,2], 1e-2)
q0 = exatn.getTensor('Q0')
q1 = exatn.getTensor('Q1')
q2 = exatn.getTensor('Q2')
q3 = exatn.getTensor('Q3')
h01 = exatn.getTensor('H01')
h12 = exatn.getTensor('H12')
h23 = exatn.getTensor('H23')
z0 = exatn.getTensor('Z0')
ham = exatn.TensorOperator('Hamiltonian')
ham.appendComponent(h01, [[0,0],[1,1]], [[0,2],[1,3]], 1.0)
ham.appendComponent(h12, [[1,0],[2,1]], [[1,2],[2,3]], 1.0)
ham.appendComponent(h23, [[2,0],[3,1]], [[2,2],[3,3]], 1.0)
mps_ket = exatn.TensorNetwork('MPS', 'Z0(i0,i1,i2,i3)+=Q0(i0,j0)*Q1(j0,i1,j1)*Q2(j1,i2,j2)*Q3(j2,i3)', {'Z0':z0, 'Q0':q0, 'Q1':q1, 'Q2':q2, 'Q3':q3})
ket = exatn.TensorExpansion()
ket.appendComponent(mps_ket, 1.0)
ket.rename('MPSket')
bra = exatn.TensorExpansion(ket)
bra.conjugate()
bra.rename('MPSbra')
ham_ket = exatn.TensorExpansion(ket, ham)
ham_ket.rename('HamMPSket')
closed_prod = exatn.TensorExpansion(ham_ket, bra)
closed_prod.rename('MPSbraHamMPSket')
closed_prod.printIt()
deriv_q1 = exatn.TensorExpansion(closed_prod, 'Q1', True)
deriv_q1.rename('DerivativeQ1')
exatn.createTensor('AC0')
accumulator0 = exatn.getTensor('AC0');
exatn.createTensor('AC1',[2,2,4], 0.0)
accumulator1 = exatn.getTensor('AC1');
exatn.evaluate(closed_prod, accumulator0)
exatn.evaluate(deriv_q1, accumulator1)
print(exatn.getLocalTensor('AC0'))
[print(exatn.getLocalTensor(c.network.getTensor(0).getName())) for c in closed_prod]
)""");
}
int main(int argc, char **argv) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment