Loading python/examples/qaoa_circuit.py +26 −7 Original line number Diff line number Diff line Loading @@ -4,7 +4,9 @@ from qcor import * import numpy as np # Define a Bell kernel from types import MethodType # Define a QAOA kernel with variational parameters (theta and beta angles) @qjit def qaoa_circ(q: qreg, cost_ham: PauliOperator, nbSteps: int, theta: List[float], beta: List[float]): # Start off in the uniform superposition Loading @@ -24,12 +26,29 @@ def qaoa_circ(q: qreg, cost_ham: PauliOperator, nbSteps: int, theta: List[float] exp_i_theta(q, beta[step], ref_ham_term) # Allocate 4 qubits q = qalloc(2) q = qalloc(4) n_steps = 3 # Use the standard parameterization scheme: one param per step theta_angle = np.random.rand(n_steps) beta_angle = np.random.rand(n_steps) # Hamiltonion: H = -5.0 - 0.5 * (Z(0) - Z(3) - Z(1) * Z(2)) - Z(2) + 2 * Z(0) * Z(2) + 2.5 * Z(2) * Z(3) comp = qaoa_circ.extract_composite(q, H, n_steps, theta_angle, beta_angle) print(comp.toString()) # Custom arg_translator in a Pythonic way def qaoa_translate(self, q: qreg, x: List[float]): ret_dict = {} ret_dict["q"] = q ret_dict["cost_ham"] = H ret_dict["nbSteps"] = n_steps ret_dict["theta"] = x[:n_steps] ret_dict["beta"] = x[n_steps:] return ret_dict # Rebind arg translate: qaoa_circ.translate = MethodType(qaoa_translate, qjit) # Use the standard parameterization scheme: # one theta + one beta per step n_params = 2 * n_steps obj = createObjectiveFunction(qaoa_circ, H, n_params) # Run optimization optimizer = createOptimizer('nlopt', {'initial-parameters': np.random.rand(n_params)}) results = optimizer.optimize(obj) python/py-qcor.cpp +3 −2 Original line number Diff line number Diff line Loading @@ -76,8 +76,9 @@ class KernelArgDictToHeterogeneousMap { }; // Add type name to this list to support receiving from Python. using PyHeterogeneousMapTypes = xacc::Variant<bool, int, double, std::string, std::shared_ptr<qcor::Optimizer>>; using PyHeterogeneousMapTypes = xacc::Variant<bool, int, double, std::string, std::shared_ptr<qcor::Optimizer>, std::vector<double>>; using PyHeterogeneousMap = std::map<std::string, PyHeterogeneousMapTypes>; // Helper to convert a Python *dict* (as a map of variants) into a native Loading Loading
python/examples/qaoa_circuit.py +26 −7 Original line number Diff line number Diff line Loading @@ -4,7 +4,9 @@ from qcor import * import numpy as np # Define a Bell kernel from types import MethodType # Define a QAOA kernel with variational parameters (theta and beta angles) @qjit def qaoa_circ(q: qreg, cost_ham: PauliOperator, nbSteps: int, theta: List[float], beta: List[float]): # Start off in the uniform superposition Loading @@ -24,12 +26,29 @@ def qaoa_circ(q: qreg, cost_ham: PauliOperator, nbSteps: int, theta: List[float] exp_i_theta(q, beta[step], ref_ham_term) # Allocate 4 qubits q = qalloc(2) q = qalloc(4) n_steps = 3 # Use the standard parameterization scheme: one param per step theta_angle = np.random.rand(n_steps) beta_angle = np.random.rand(n_steps) # Hamiltonion: H = -5.0 - 0.5 * (Z(0) - Z(3) - Z(1) * Z(2)) - Z(2) + 2 * Z(0) * Z(2) + 2.5 * Z(2) * Z(3) comp = qaoa_circ.extract_composite(q, H, n_steps, theta_angle, beta_angle) print(comp.toString()) # Custom arg_translator in a Pythonic way def qaoa_translate(self, q: qreg, x: List[float]): ret_dict = {} ret_dict["q"] = q ret_dict["cost_ham"] = H ret_dict["nbSteps"] = n_steps ret_dict["theta"] = x[:n_steps] ret_dict["beta"] = x[n_steps:] return ret_dict # Rebind arg translate: qaoa_circ.translate = MethodType(qaoa_translate, qjit) # Use the standard parameterization scheme: # one theta + one beta per step n_params = 2 * n_steps obj = createObjectiveFunction(qaoa_circ, H, n_params) # Run optimization optimizer = createOptimizer('nlopt', {'initial-parameters': np.random.rand(n_params)}) results = optimizer.optimize(obj)
python/py-qcor.cpp +3 −2 Original line number Diff line number Diff line Loading @@ -76,8 +76,9 @@ class KernelArgDictToHeterogeneousMap { }; // Add type name to this list to support receiving from Python. using PyHeterogeneousMapTypes = xacc::Variant<bool, int, double, std::string, std::shared_ptr<qcor::Optimizer>>; using PyHeterogeneousMapTypes = xacc::Variant<bool, int, double, std::string, std::shared_ptr<qcor::Optimizer>, std::vector<double>>; using PyHeterogeneousMap = std::map<std::string, PyHeterogeneousMapTypes>; // Helper to convert a Python *dict* (as a map of variants) into a native Loading