Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Qobj creation function metadata setting #2388

Merged
merged 7 commits into from May 13, 2024

Conversation

Ericgig
Copy link
Member

@Ericgig Ericgig commented Apr 9, 2024

Description
Many of the operator functions did not set the isherm flag.
Not having the flag set would cause issue with jax.jit.

  • Set the isherm flag for almost every functions in operators.py and gates.py.
    • qdiags flags are only set when only one diagonal in used.
    • I did not touch functions creating operators from other operators ( commutator, squeezing)
  • Set the isunitary flags for all these also.
  • Updated the gates dtype parameter to use the default_dtype settings.
  • Updated the sigma operators to have a dtype parameter.
  • Added tests for all of these.

qutip/core/operators.py Show resolved Hide resolved
qutip/core/operators.py Show resolved Hide resolved
qutip/core/operators.py Show resolved Hide resolved
qutip/core/qobj.py Show resolved Hide resolved
@@ -142,3 +150,49 @@ def test_gate_normalises_pauli_group(self, gate):
del pauli_gates[i]
break
assert len(pauli_gates) == 0


@pytest.mark.parametrize("dtype", [qutip.data.Dense, qutip.data.CSR])
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we just check the Dia type here too? I guess the with qutip.CoreOptions(default_dtype="dia") around clifford = ... above was to check the Dia case, but I would prefer not to have to stare at the test to tell that Dia is tested too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I choose 2 types so we don't only get the default, but not all type since we are not testing the conversion code, just that it is used. Adding dia would just add more test without testing anything new.

qutip/core/gates.py Outdated Show resolved Hide resolved
qutip/core/gates.py Outdated Show resolved Hide resolved
op.to(dtype)
for op in map(
partial(reduce, mul),
product(_powers(E, 3), _powers(X, 2), _powers(S, 4)),
)
]
for gate in gates:
gate.isherm
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Copy link
Member Author

Choose a reason for hiding this comment

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

Set the flags, since there are 24 of them with some being hermitian other not, I call the method compute them. Since they are all 2x2, it does not that a significant time.

@coveralls
Copy link

coveralls commented Apr 10, 2024

Coverage Status

coverage: 86.22% (+0.2%) from 86.048%
when pulling e5d2877 on Ericgig:misc.qobj_creation_metadata
into 8035590 on qutip:master.

qutip/core/gates.py Show resolved Hide resolved
return Qobj(
[
[np.cos(phi / 2), -1j * np.sin(phi / 2)],
[-1j * np.sin(phi / 2), np.cos(phi / 2)],
]
],
isherm=(phi % (2 * np.pi) <= settings.core["atol"]),
Copy link
Member

Choose a reason for hiding this comment

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

This is nice but I fear that it may again cause problems with jit. I'm fine just leave it None here. The same for other rotations.

@@ -72,10 +83,11 @@ def cz_gate(*, dtype="csr"):
result : :class:`.Qobj`
Quantum object for operator describing the rotation.
"""
return qdiags([1, 1, 1, -1], dims=[[2, 2], [2, 2]], dtype=dtype)
dtype = dtype or settings.core["default_dtype"] or _data.CSR
return qdiags([1, 1, 1, -1], dims=_DIMS_2_QB, dtype=dtype)
Copy link
Member

Choose a reason for hiding this comment

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

How does qdiags handle the isherms property? Will it check if the input is real? If so, maybe for those we could manually set the isherms and isunitary.

Copy link
Member Author

Choose a reason for hiding this comment

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

When there is only one diagonal, it sets it automatically.
There is only one case in our code base where this is not the case (and it's not in the tests...).
Improving tests for it and adding manual flag setting could make a good first issue.

@Ericgig Ericgig requested a review from hodgestar April 10, 2024 12:48
qutip/core/qobj.py Outdated Show resolved Hide resolved
qutip/entropy.py Outdated Show resolved Hide resolved
Copy link
Contributor

@hodgestar hodgestar left a comment

Choose a reason for hiding this comment

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

Other than my two small suggestions, looks good.

qutip/tests/test_entropy.py Outdated Show resolved Hide resolved
Co-authored-by: Simon Cross <hodgestar+github@gmail.com>
@Ericgig Ericgig merged commit 8ab189e into qutip:master May 13, 2024
11 of 12 checks passed
@Ericgig Ericgig deleted the misc.qobj_creation_metadata branch May 13, 2024 14:38
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.

None yet

4 participants