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

Resource methods for adjoints, controlls, and pows. #6648

Open
wants to merge 548 commits into
base: master
Choose a base branch
from

Conversation

willjmax
Copy link
Contributor

No description provided.

Jaybsoni and others added 30 commits November 18, 2024 17:48
Base automatically changed from resource_symbolic_ops to master December 6, 2024 19:41
Copy link
Contributor Author

@willjmax willjmax left a comment

Choose a reason for hiding this comment

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

Looks mostly good. I left a few comments. I'll give this another look once you are finished adding tests.

Comment on lines 37 to 50
@staticmethod
def adjoint_resource_decomp() -> Dict[re.CompressedResourceOp, int]:
return {}

@staticmethod
def controlled_resource_decomp(
num_ctrl_wires, num_ctrl_values, num_work_wires
) -> Dict[re.CompressedResourceOp, int]:
return {}

@staticmethod
def pow_resource_decomp(z) -> Dict[re.CompressedResourceOp, int]:
return {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think these should return {cls.resource_rep(): 1}. This comment applies to the rest of the codebase as well. I think only the identity's resource decomposition should return {} and anything else currently returning {} should be changed to return {re.ResourceIdentity.resource_rep(): 1}. Users might want to track how many times the identity operator shows up in their circuit, but this can't happen with return {}. The performance gain in returning {} is probably negligible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Changed 👍🏼

Comment on lines 71 to 76
def controlled_resource_decomp(
num_ctrl_wires, num_ctrl_values, num_work_wires
) -> Dict[re.CompressedResourceOp, int]:
if num_ctrl_values == 0:
return {re.ResourcePhaseShift.resource_rep(): 1}
raise re.ResourcesNotDefined
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we not need to check if num_ctrl_wires == 1? or do all multi controlled global phases decompose to the same phase shift operator?

Copy link
Contributor

Choose a reason for hiding this comment

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

I modified the decomposition to be general now

Comment on lines -125 to +192
gate_types = {rx: 1, ry: 1, rz: 1}
gate_types = {ry: 1, rz: 2}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why is this change needed?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it was just implemented incorrectly?

Comment on lines +63 to +77
if num_ctrl_values == 0:
cnot = re.ResourceCNOT.resource_rep()
ctrl_rz = re.ResourceControlled.resource_rep(
base_class=re.ResourceRZ,
base_params={},
num_ctrl_wires=num_ctrl_wires,
num_ctrl_values=num_ctrl_values,
num_work_wires=num_work_wires,
)

gate_types = {}
gate_types[cnot] = 2 * (num_wires - 1)
gate_types[ctrl_rz] = 1

return gate_types
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we not handle num_ctrl_values > 0 by adding X gates?

Copy link
Contributor

Choose a reason for hiding this comment

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

We can! However if you look at the logic in re.ResourceControlled it first tries to expand the op, if it encounters an error, it adds the Xs and then tries the controlled method again. So here I am relying on that logic to add the Xs

num_work_wires,
pauli_word,
) -> Dict[re.CompressedResourceOp, int]:
if num_ctrl_values == 0:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same comment as above

num_ctrl_values,
num_work_wires,
) -> Dict[re.CompressedResourceOp, int]:
if num_ctrl_values == 0:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same

Comment on lines -59 to +122
re.ResourceS.resource_rep(): 4,
re.ResourceS.resource_rep(): 1,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because previously we were using S to count both S and Adjoint(S)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

"""Tests for ResourceX"""

def test_resources(self):
"""Test that ResourceT does not implement a decomposition"""
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove this comment

Copy link
Contributor

Choose a reason for hiding this comment

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

Removed 👍🏼

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.

2 participants