Commit 69b22cba authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added hook to test Python bindings



To provide basic coverage of the Python API.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent 8d577572
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ compile_commands.json
/docs/doxygen/html**
**/*.dat
**/.antlr
__pycache__/
# IDE files
.vscode/*
.theia/*
+5 −1
Original line number Diff line number Diff line
@@ -25,3 +25,7 @@ endif()

install(FILES qcor.py DESTINATION ${CMAKE_INSTALL_PREFIX})
install(TARGETS _pyqcor DESTINATION ${CMAKE_INSTALL_PREFIX})

if (QCOR_BUILD_TESTS)
  add_subdirectory(tests)
endif()
 No newline at end of file
+4 −0
Original line number Diff line number Diff line
add_test (NAME qcor_qsim_python_bindings
  COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_qsim.py
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
 No newline at end of file
+33 −0
Original line number Diff line number Diff line
import sys, os
from pathlib import Path
sys.path.insert(1, str(Path.home()) + "/.xacc")

import unittest
from qcor import *
import numpy as np

class TestWorkflows(unittest.TestCase):
    def test_td_workflow(self):
      # Time-dependent Hamiltonian: 
      # Returns the Pauli operators at a time point.
      def td_hamiltonian(t):
        Jz = 2 * np.pi * 2.86265 * 1e-3
        epsilon = Jz
        omega = 4.8 * 2 * np.pi * 1e-3
        return -Jz * Z(0) * Z(1)  - Jz * Z(1) * Z(2) + (-epsilon * np.cos(omega * t)) * (X(0) + X(1) + X(2)) 

      # Observable = average magnetization
      observable = (1.0 / 3.0) * (Z(0) + Z(1) + Z(2))
      problemModel = qsim.ModelBuilder.createModel(observable, td_hamiltonian)
      nbSteps = 100
      workflow = qsim.getWorkflow(
        "td-evolution", {"method": "trotter", "dt": 3.0, "steps": nbSteps})
      result = workflow.execute(problemModel)
      self.assertEqual(len(result["exp-vals"]), nbSteps + 1)
      self.assertAlmostEqual(result["exp-vals"][0], 1.0, places=1)
      self.assertAlmostEqual(result["exp-vals"][nbSteps], 0.5, places=1)

if __name__ == '__main__':
  # Set up QCOR runtime
  Initialize(qpu="qpp")
  unittest.main()
 No newline at end of file