27 #define PY_SSIZE_T_CLEAN 31 #include "structmember.h" 56 return new R( qbit_num, target_qbit );
89 Py_TYPE(
self)->tp_free((PyObject *)
self);
103 return (PyObject *)
self;
116 static char *kwlist[] = {(
char*)
"qbit_num", (
char*)
"target_qbit", NULL};
120 if (PyArray_API == NULL) {
124 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"|ii", kwlist,
125 &qbit_num, &target_qbit))
128 if (qbit_num != -1 && target_qbit != -1) {
129 self->gate =
create_R( qbit_num, target_qbit );
141 PyArrayObject * parameters_arr = NULL;
145 if (!PyArg_ParseTuple(args,
"|O", ¶meters_arr ))
146 return Py_BuildValue(
"i", -1);
149 if ( PyArray_IS_C_CONTIGUOUS(parameters_arr) ) {
150 Py_INCREF(parameters_arr);
153 parameters_arr = (PyArrayObject*)PyArray_FROM_OTF( (PyObject*)parameters_arr, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
161 Matrix R_mtx =
self->gate->get_matrix( parameters_mtx, parallel );
168 Py_DECREF(parameters_arr);
181 PyArrayObject * parameters_arr = NULL;
182 PyArrayObject * unitary_arg = NULL;
187 if (!PyArg_ParseTuple(args,
"|OO", ¶meters_arr, &unitary_arg ))
188 return Py_BuildValue(
"i", -1);
190 if ( PyArray_IS_C_CONTIGUOUS(parameters_arr) ) {
191 Py_INCREF(parameters_arr);
194 parameters_arr = (PyArrayObject*)PyArray_FROM_OTF( (PyObject*)parameters_arr, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
201 if ( unitary_arg == NULL ) {
202 PyErr_SetString(PyExc_Exception,
"Input matrix was not given");
206 PyArrayObject*
unitary = (PyArrayObject*)PyArray_FROM_OTF( (PyObject*)unitary_arg, NPY_COMPLEX128, NPY_ARRAY_IN_ARRAY);
209 if ( !PyArray_IS_C_CONTIGUOUS(unitary) ) {
210 PyErr_SetString(PyExc_Exception,
"input mtrix is not memory contiguous");
218 self->gate->apply_to( parameters_mtx, unitary_mtx, parallel );
220 if (unitary_mtx.
data != PyArray_DATA(unitary)) {
224 Py_DECREF(parameters_arr);
227 return Py_BuildValue(
"i", 0);
241 double ThetaOver2,
Phi;
244 if (!PyArg_ParseTuple(args,
"|dd", &ThetaOver2,&Phi ))
245 return Py_BuildValue(
"i", -1);
250 double phi0 = Phi-M_PI/2;
251 double lambda0 = -1.*Phi + M_PI/2;
253 Matrix R_1qbit_ =
self->gate->calc_one_qubit_u3(ThetaOver2, phi0, lambda0 );
272 return Py_BuildValue(
"i", parameter_num);
283 int start_index =
self->gate->get_parameter_start_idx();
285 return Py_BuildValue(
"i", start_index);
300 return Py_BuildValue(
"i", target_qbit);
313 return Py_BuildValue(
"i", control_qbit);
322 int target_qbit_in = -1;
323 if (!PyArg_ParseTuple(args,
"|i", &target_qbit_in ))
324 return Py_BuildValue(
"i", -1);
325 self->gate->set_target_qbit(target_qbit_in);
327 return Py_BuildValue(
"i", 0);
337 PyArrayObject * parameters_arr = NULL;
341 if (!PyArg_ParseTuple(args,
"|O", ¶meters_arr ))
342 return Py_BuildValue(
"i", -1);
345 if ( PyArray_IS_C_CONTIGUOUS(parameters_arr) ) {
346 Py_INCREF(parameters_arr);
349 parameters_arr = (PyArrayObject*)PyArray_FROM_OTF( (PyObject*)parameters_arr, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
360 extracted_parameters =
self->gate->extract_parameters( parameters_mtx );
362 catch (std::string err) {
363 PyErr_SetString(PyExc_Exception, err.c_str());
367 std::string err(
"Invalid pointer to circuit class");
368 PyErr_SetString(PyExc_Exception, err.c_str());
378 return extracted_parameters_py;
396 "Method to get the matrix of the operation." 399 "Call to apply the gate on the input matrix." 402 "Call to calculate the gate matrix acting on a single qbit space." 405 "Call to get the number of free parameters in the gate." 408 "Call to get the starting index of the parameters in the parameter array corresponding to the circuit in which the current gate is incorporated." 411 "Call to get the target qbit." 414 "Call to get the control qbit (returns with -1 if no control qbit is used in the gate)." 417 "Call to set the target qbit." 420 "Call to extract the paramaters corresponding to the gate, from a parameter array associated to the circuit in which the gate is embedded." 430 PyVarObject_HEAD_INIT(NULL, 0)
431 "qgd_R_Wrapper.qgd_R_Wrapper",
435 #if PY_VERSION_HEX < 0x030800b4 438 #if PY_VERSION_HEX >= 0x030800b4 443 #if PY_MAJOR_VERSION < 3 446 #if PY_MAJOR_VERSION >= 3 459 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
460 "Object to represent a R gate of the QGD package.",
487 #
if PY_VERSION_HEX >= 0x030400a1
490 #
if PY_VERSION_HEX >= 0x030800b1
493 #
if PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000
503 PyModuleDef_HEAD_INIT,
505 "Python binding for QGD R gate",
521 if (PyType_Ready(& qgd_R_Wrapper_Type) < 0)
524 m = PyModule_Create(& qgd_R_Wrapper_Module);
528 Py_INCREF(& qgd_R_Wrapper_Type);
529 if (PyModule_AddObject(m,
"qgd_R_Wrapper", (PyObject *) & qgd_R_Wrapper_Type) < 0) {
530 Py_DECREF(& qgd_R_Wrapper_Type);
R * create_R(int qbit_num, int target_qbit)
Creates an instance of class N_Qubit_Decomposition and return with a pointer pointing to the class in...
parameter_num
[set adaptive gate structure]
Header file for a class representing a rotation gate around the X axis.
static PyMemberDef qgd_R_Wrapper_members[]
Structure containing metadata about the members of class qgd_RX_Wrapper.
static PyModuleDef qgd_R_Wrapper_Module
Structure containing metadata about the module.
Type definition of the qgd_RX_Wrapper Python class of the qgd_RX_Wrapper module.
static PyMethodDef qgd_R_Wrapper_methods[]
Structure containing metadata about the methods of class qgd_RX_Wrapper.
static void qgd_R_Wrapper_dealloc(qgd_R_Wrapper *self)
Method called when a python instance of the class qgd_RX_Wrapper is destroyed.
static PyObject * qgd_R_Wrapper_apply_to(qgd_R_Wrapper *self, PyObject *args)
Call to apply the gate operation on the inut matrix.
Matrix_real numpy2matrix_real(PyArrayObject *arr)
Call to create a PIC matrix_real representation of a numpy array.
static PyObject * qgd_R_Wrapper_get_Parameter_Num(qgd_R_Wrapper *self)
Call to get the number of free parameters in the gate.
scalar * data
pointer to the stored data
void release_R(R *instance)
Call to deallocate an instance of N_Qubit_Decomposition class.
PyObject * matrix_real_to_numpy(Matrix_real &mtx)
Call to make a numpy array from an instance of matrix class.
static PyObject * qgd_R_Wrapper_get_Control_Qbit(qgd_R_Wrapper *self)
Call to get the control qbit (returns with -1 if no control qbit is used in the gate) ...
static PyObject * qgd_R_Wrapper_get_Matrix(qgd_R_Wrapper *self, PyObject *args)
Extract the optimized parameters.
static PyObject * qgd_R_Wrapper_set_Target_Qbit(qgd_R_Wrapper *self, PyObject *args)
Call to set the target qbit.
PyMODINIT_FUNC PyInit_qgd_R_Wrapper(void)
Method called when the Python module is initialized.
static PyObject * qgd_R_Wrapper_get_Gate_Kernel(qgd_R_Wrapper *self, PyObject *args)
Calculate the matrix of a U3 gate gate corresponding to the given parameters acting on a single qbit ...
static PyObject * qgd_R_Wrapper_get_Target_Qbit(qgd_R_Wrapper *self)
Call to get the target qbit.
PyObject_HEAD R * gate
Pointer to the C++ class of the RX gate.
void set_owner(bool owner_in)
Call to set the current class instance to be (or not to be) the owner of the stored data array...
Structure type representing complex numbers in the SQUANDER package.
static int qgd_R_Wrapper_init(qgd_R_Wrapper *self, PyObject *args, PyObject *kwds)
Method called when a python instance of the class qgd_RX_Wrapper is initialized.
Class to store data of complex arrays and its properties.
static PyObject * qgd_R_Wrapper_Extract_Parameters(qgd_R_Wrapper *self, PyObject *args)
Call to extract the paramaters corresponding to the gate, from a parameter array associated to the ci...
int size() const
Call to get the number of the allocated elements.
static PyTypeObject qgd_R_Wrapper_Type
A structure describing the type of the class qgd_RX_Wrapper.
static PyObject * qgd_R_Wrapper_get_Parameter_Start_Index(qgd_R_Wrapper *self)
Call to get the starting index of the parameters in the parameter array corresponding to the circuit ...
A class representing a U3 gate.
static PyObject * qgd_R_Wrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Method called when a python instance of the class qgd_RX_Wrapper is allocated.
Matrix numpy2matrix(PyArrayObject *arr)
Call to create a PIC matrix representation of a numpy array.
PyObject * matrix_to_numpy(Matrix &mtx)
Call to make a numpy array from an instance of matrix class.
Class to store data of complex arrays and its properties.