Skip to content

Conversation

@mcjamieson
Copy link

Updates to migrate pyQuEST to QuEST v4

Copy link
Owner

@rrmeister rrmeister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for the PR, Maurice! Looks very promising for porting pyQuEST to QuESTv4. I've had a skim through the changes and have left some comments. Some are quite nit-picky, but others are rather critical. Have a look and see if they all make sense.

Cheers!
Richard

pyquest/core.pyx Outdated
quest.seedQuEST(&(self.c_env), seeds, num_seeds)
free(seeds)
def seeds(self, value):
cdef unsigned[:] seed_array = np.asarray(value, dtype=np.uint32)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting to an unsigned type without checking for negative values will underflow and give a numpy warning. Probably better to explicitly check and throw an appropriate error when trying to seed with a negative value.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added check for negative values

pyquest/core.pyx Outdated
free(seeds)
def seeds(self, value):
cdef unsigned[:] seed_array = np.asarray(value, dtype=np.uint32)
if seed_array.size != quest.getNumSeeds():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The seed array for QuEST doesn't need to remain constant in size.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed constant size check

pyquest/core.pyx Outdated

quest.setQuregToWeightedSum(res_reg.c_register, &(coeffs[0]), &(quregs[0]), 2)

# Result has scaling factor 1 since it's the difference
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since res_reg is a new Register, its scaling factor is already 1, no need to set this expliticly.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed explicit value setting

pyquest/core.pyx Outdated

quest.setQuregToWeightedSum(res_reg.c_register, &(coeffs[0]), &(quregs[0]), 2)

# Result has scaling factor 1 since it's the sum
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in L375.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed explicit setting

pyquest/core.pyx Outdated
# Create result = 1.0 * left_scaling * left - 1.0 * right_scaling * right
coeffs[0] = left_reg._scaling_factor
coeffs[1].real = -right_reg._scaling_factor.real
coeffs[1].imag = -right_reg._scaling_factor.imag
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need the real and imag parts set separately? I would have thought

coeffs[1] = -right_reg._scaling_factor

would suffice.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified as suggested

for k in range(matrix_dim):
self._real[k] = (<ComplexMatrix2*>self._matrix).real[k]
self._imag[k] = (<ComplexMatrix2*>self._matrix).imag[k]
self._matrix = malloc(sizeof(quest.CompMatr1))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not initialise all fields of the CompMatr1 struct and will cause an error when applied to a Register.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

for m in range(arr.shape[1]):
self._real[k][m] = arr[k, m].real
self._imag[k][m] = arr[k, m].imag
self._set_matrix_element(k, m, <qcomp>(arr[k, m].real + 1j * arr[k, m].imag))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth checking if manually assembling a complex number here is really necessary or if the cast to qcomp would take care of that. Also in all following functions.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usingqcomp cast

quest.initDiagonalOp(self._diag_op, &real_el[0], &imag_el[0])
# Convert Python complex array to qcomp array
num_elems = diag_elements.size
qcomp_el = np.ascontiguousarray(diag_elements, dtype=np.complex128)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use the data type QuEST uses, not just always complex128.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now pyquest.core.np_qcomp

for k in range(matrix_dim):
self._real[k] = &(<ComplexMatrix2*>self._matrix).real[k][0]
self._imag[k] = &(<ComplexMatrix2*>self._matrix).imag[k][0]
self._matrix = malloc(sizeof(quest.CompMatr1))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not properly init all fields of CompMatr1.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialised fully

for k in range(matrix_dim):
self._real[k] = &(<ComplexMatrix4*>self._matrix).real[k][0]
self._imag[k] = &(<ComplexMatrix4*>self._matrix).imag[k][0]
self._matrix = malloc(sizeof(quest.CompMatr2))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not properly init all fields of CompMatr2.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialised fully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants