27 #define PY_SSIZE_T_CLEAN 31 #include "structmember.h" 56 return new CROT( qbit_num, target_qbit, control_qbit, subtype_in );
89 Py_TYPE(
self)->tp_free((PyObject *)
self);
103 return (PyObject *)
self;
116 static char *kwlist[] = {(
char*)
"qbit_num", (
char*)
"target_qbit", (
char*)
"control_qbit", (
char*)
"subtype",NULL};
120 PyObject* subtype_arg = NULL;
122 if (PyArray_API == NULL) {
126 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"|iiiO", kwlist,
127 &qbit_num, &target_qbit, &control_qbit,&subtype_arg))
129 PyObject* subtype_string = PyObject_Str(subtype_arg);
130 PyObject* subtype_string_unicode = PyUnicode_AsEncodedString(subtype_string,
"utf-8",
"~E~");
131 const char* subtype_C = PyBytes_AS_STRING(subtype_string_unicode);
133 if ( strcmp(
"control_r", subtype_C) == 0 || strcmp(
"CONTROL_R", subtype_C) == 0) {
136 else if ( strcmp(
"control_opposite", subtype_C)==0 || strcmp(
"CONTROL_OPPOSITE", subtype_C)==0) {
139 else if ( strcmp(
"control_independent", subtype_C)==0 || strcmp(
"CONTROL_INDEPENDENT", subtype_C)==0) {
142 if (qbit_num != -1 && target_qbit != -1) {
143 self->gate =
create_CROT( qbit_num, target_qbit, control_qbit,qgd_subtype );
157 PyArrayObject * parameters_arr = NULL;
158 PyArrayObject * unitary_arg = NULL;
163 if (!PyArg_ParseTuple(args,
"|OO", ¶meters_arr, &unitary_arg ))
164 return Py_BuildValue(
"i", -1);
166 if ( PyArray_IS_C_CONTIGUOUS(parameters_arr) ) {
167 Py_INCREF(parameters_arr);
170 parameters_arr = (PyArrayObject*)PyArray_FROM_OTF( (PyObject*)parameters_arr, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
177 if ( unitary_arg == NULL ) {
178 PyErr_SetString(PyExc_Exception,
"Input matrix was not given");
182 PyArrayObject*
unitary = (PyArrayObject*)PyArray_FROM_OTF( (PyObject*)unitary_arg, NPY_COMPLEX128, NPY_ARRAY_IN_ARRAY);
185 if ( !PyArray_IS_C_CONTIGUOUS(unitary) ) {
186 PyErr_SetString(PyExc_Exception,
"input mtrix is not memory contiguous");
194 self->gate->apply_to( parameters_mtx, unitary_mtx, parallel );
196 if (unitary_mtx.
data != PyArray_DATA(unitary)) {
200 Py_DECREF(parameters_arr);
203 return Py_BuildValue(
"i", 0);
217 return Py_BuildValue(
"i", parameter_num);
228 int start_index =
self->gate->get_parameter_start_idx();
230 return Py_BuildValue(
"i", start_index);
244 return Py_BuildValue(
"i", target_qbit);
257 return Py_BuildValue(
"i", control_qbit);
266 int target_qbit_in = -1;
267 if (!PyArg_ParseTuple(args,
"|i", &target_qbit_in ))
268 return Py_BuildValue(
"i", -1);
269 self->gate->set_target_qbit(target_qbit_in);
271 return Py_BuildValue(
"i", 0);
280 int control_qbit_in = -1;
281 if (!PyArg_ParseTuple(args,
"|i", &control_qbit_in ))
282 return Py_BuildValue(
"i", -1);
283 self->gate->set_control_qbit(control_qbit_in);
285 return Py_BuildValue(
"i", 0);
294 PyArrayObject * parameters_arr = NULL;
298 if (!PyArg_ParseTuple(args,
"|O", ¶meters_arr ))
299 return Py_BuildValue(
"i", -1);
302 if ( PyArray_IS_C_CONTIGUOUS(parameters_arr) ) {
303 Py_INCREF(parameters_arr);
306 parameters_arr = (PyArrayObject*)PyArray_FROM_OTF( (PyObject*)parameters_arr, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
317 extracted_parameters =
self->gate->extract_parameters( parameters_mtx );
319 catch (std::string err) {
320 PyErr_SetString(PyExc_Exception, err.c_str());
324 std::string err(
"Invalid pointer to circuit class");
325 PyErr_SetString(PyExc_Exception, err.c_str());
335 return extracted_parameters_py;
352 "Call to apply the gate on the input matrix." 355 "Call to get the number of free parameters in the gate." 358 "Call to get the starting index of the parameters in the parameter array corresponding to the circuit in which the current gate is incorporated." 361 "Call to get the target qbit." 364 "Call to get the control qbit (returns with -1 if no control qbit is used in the gate)." 367 "Call to set the target qbit." 370 "Call to set the control qbit." 373 "Call to extract the paramaters corresponding to the gate, from a parameter array associated to the circuit in which the gate is embedded." 383 PyVarObject_HEAD_INIT(NULL, 0)
384 "qgd_CROT_Wrapper.qgd_CROT_Wrapper",
388 #if PY_VERSION_HEX < 0x030800b4 391 #if PY_VERSION_HEX >= 0x030800b4 396 #if PY_MAJOR_VERSION < 3 399 #if PY_MAJOR_VERSION >= 3 412 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
413 "Object to represent a RY gate of the QGD package.",
440 #
if PY_VERSION_HEX >= 0x030400a1
443 #
if PY_VERSION_HEX >= 0x030800b1
446 #
if PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000
456 PyModuleDef_HEAD_INIT,
458 "Python binding for QGD RY gate",
473 if (PyType_Ready(& qgd_CROT_Wrapper_Type) < 0)
476 m = PyModule_Create(& qgd_CROT_Wrapper_Module);
480 Py_INCREF(& qgd_CROT_Wrapper_Type);
481 if (PyModule_AddObject(m,
"qgd_CROT_Wrapper", (PyObject *) & qgd_CROT_Wrapper_Type) < 0) {
482 Py_DECREF(& qgd_CROT_Wrapper_Type);
parameter_num
[set adaptive gate structure]
void release_CROT(CROT *instance)
Call to deallocate an instance of N_Qubit_Decomposition class.
static void qgd_CROT_Wrapper_dealloc(qgd_CROT_Wrapper *self)
Method called when a python instance of the class qgd_CROT_Wrapper is destroyed.
static int qgd_CROT_Wrapper_init(qgd_CROT_Wrapper *self, PyObject *args, PyObject *kwds)
Method called when a python instance of the class qgd_CROT_Wrapper is initialized.
static PyModuleDef qgd_CROT_Wrapper_Module
Structure containing metadata about the module.
static PyObject * qgd_CROT_Wrapper_apply_to(qgd_CROT_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.
Header file for a class representing a controlled rotation gate around the Y axis.
scalar * data
pointer to the stored data
static PyMemberDef qgd_CROT_Wrapper_members[]
Structure containing metadata about the members of class qgd_CROT_Wrapper.
static PyObject * qgd_CROT_Wrapper_set_Control_Qbit(qgd_CROT_Wrapper *self, PyObject *args)
Call to set the target qbit.
PyObject * matrix_real_to_numpy(Matrix_real &mtx)
Call to make a numpy array from an instance of matrix class.
A class representing a CROT gate.
static PyObject * qgd_CROT_Wrapper_get_Parameter_Num(qgd_CROT_Wrapper *self)
Call to get the number of free parameters in the gate.
static PyObject * qgd_CROT_Wrapper_Extract_Parameters(qgd_CROT_Wrapper *self, PyObject *args)
Call to extract the paramaters corresponding to the gate, from a parameter array associated to the ci...
static PyTypeObject qgd_CROT_Wrapper_Type
A structure describing the type of the class qgd_CROT_Wrapper.
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.
Class to store data of complex arrays and its properties.
int size() const
Call to get the number of the allocated elements.
CROT * create_CROT(int qbit_num, int target_qbit, int control_qbit, crot_type subtype_in)
Creates an instance of class N_Qubit_Decomposition and return with a pointer pointing to the class in...
static PyObject * qgd_CROT_Wrapper_get_Control_Qbit(qgd_CROT_Wrapper *self)
Call to get the control qbit (returns with -1 if no control qbit is used in the gate) ...
static PyObject * qgd_CROT_Wrapper_get_Target_Qbit(qgd_CROT_Wrapper *self)
Call to get the target qbit.
PyMODINIT_FUNC PyInit_qgd_CROT_Wrapper(void)
Method called when the Python module is initialized.
static PyObject * qgd_CROT_Wrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Method called when a python instance of the class qgd_CROT_Wrapper is allocated.
Type definition of the qgd_CROT Python class of the qgd_CROT module.
static PyObject * qgd_CROT_Wrapper_get_Parameter_Start_Index(qgd_CROT_Wrapper *self)
Call to get the starting index of the parameters in the parameter array corresponding to the circuit ...
static PyObject * qgd_CROT_Wrapper_set_Target_Qbit(qgd_CROT_Wrapper *self, PyObject *args)
Call to set the target qbit.
Matrix numpy2matrix(PyArrayObject *arr)
Call to create a PIC matrix representation of a numpy array.
static PyMethodDef qgd_CROT_Wrapper_methods[]
Structure containing metadata about the methods of class qgd_CROT_Wrapper.
Class to store data of complex arrays and its properties.