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

Custom package and circuit interface #1377

Closed
1 task done
refraction-ray opened this issue Jun 30, 2022 · 7 comments · Fixed by #1850
Closed
1 task done

Custom package and circuit interface #1377

refraction-ray opened this issue Jun 30, 2022 · 7 comments · Fixed by #1850
Assignees
Labels
feature-request A request for a feature, tool, or workflow in Mitiq. interface-and-conversions How Mitiq interfaces with quantum software packages. unitaryhack-bounty
Milestone

Comments

@refraction-ray
Copy link

Pre-Request Checklist

  • I checked to make sure that this feature has not already been requested.

Issue Description

Currently, if we want to interface mitiq with quantum packages that not listed in interfaces submodule, we can take the following workaround:

import mypackage

mycircuit = mypackage.Circuit()

def to_cirq(mycircuit):
    # ...
    return cirq.Circuit

cirqcircuit = to_cirq(mycircuit)

def from_cirq(cirqcircuit):
    # ...
    return mypackage.Circuit()

def executor(cirqcircuit):
    mycircuit = from_cirq(cirqcircuit)
    return mypackage.run(mycircuit)
 

zne.execute_with_zne(cirqcircuit, executor)

However, this workflow is not straightforward and error-prone. Since we have defined from_cirq and to_cirq anyway, a better integration would be a hook to register these two methods to convert_to_mitiq and convert_from_mitiq. Then the above work flow will be simplified to

# after to and from mitiq registeration

mycircuit = mypackage.Circuit()

def executor(mycircuit):
    return mypackage.run(mycircuit)
 

zne.execute_with_zne(mycircuit, executor)

Namely, just native user experience as the currently supported quantum software packages.

Proposed Solution

For now, the extension on convert_to_mitiq and convert_from_mitiq is impossible since the two function are hand coded if else branch with no user customized fallback. Therefore, a possible API for the hook and the corresponding implementation may be as follows:

# the proposed API
mitiq.interfaces.register_to_mitiq(package_name="mypackage", convert_function=to_cirq)
mitiq.interfaces.register_from_mitiq(package_name="mypackage", convert_function=from_cirq)

# the impl for convert_to_mitiq
def register_to_mitiq(package_name, convert_function):
    global register_dict
    register_dict[package_name] = convert_function

def convert_to_mitiq(circuit):
    if 
    elif
    # ...
    else:
        for package_name in register_dict:
             if package_name in package:
                conversion_function = register_dict[package_name]
                break
        else:
             raise UnsupportedCircuitError
    

Additional References

@refraction-ray refraction-ray added the feature-request A request for a feature, tool, or workflow in Mitiq. label Jun 30, 2022
@github-actions
Copy link

Hello @refraction-ray, thank you for your interest in Mitiq!
If this is a bug report, please provide screenshots and/or minimum viable code to reproduce your issue, so we can do our best to help get it fixed. If you have any questions in the meantime, you can also ask us on the Unitary Fund Discord.

@andreamari andreamari added interface-and-conversions How Mitiq interfaces with quantum software packages. needs/agreed-design Needs a plan of action that is agreed upon to complete. labels Jul 4, 2022
@andreamari
Copy link
Member

Thanks @refraction-ray, we'll think about your proposal.

Honestly, the solution of your first code snippet doesn't look so bad to me.
It requires two lines of codes (manual insertion of from_cirq and to_cirq functions). The proposed solution also requires two lines of code:

mitiq.interfaces.register_to_mitiq(package_name="mypackage", convert_function=to_cirq)
mitiq.interfaces.register_from_mitiq(package_name="mypackage", convert_function=from_cirq)

Do you have strong arguments in favor of the second approach?

@refraction-ray
Copy link
Author

Thanks for your reply.

Do you have strong arguments in favor of the second approach?

Yes, consider the scenario of three parties (user, mypackage developer and mitiq developer) instead of two parties (user and mitiq developer). It is mypackage's developers responsibility to implement to_cirq, from_cirq and register these new interfaces in mitiq. All these implementation are in mypackage module (say mypackage/mitiq_interface.py). So that users of mypackage can directly use the following code (regarding mypackage similar as packages with mitiq native support)

mycircuit = mypackage.Circuit()

def executor(mycircuit):
    return mypackage.run(mycircuit)
 

zne.execute_with_zne(mycircuit, executor)

@andreamari
Copy link
Member

Ah, now I see the advantage thanks! Sounds interesting to me.
I'll ping other Mitiq devs:
@nathanshammah, @natestemen, @Misty-W

@natestemen
Copy link
Member

I personally really like the idea of allowing user-defined to_mitiq/from_mitiq functions that hook into our code. The solution proposed by Shixin seems like a good one.

@natestemen natestemen added this to the 0.25.0 milestone Mar 2, 2023
@natestemen natestemen self-assigned this Mar 3, 2023
@natestemen natestemen changed the title User defined new package and circuit interface Custom package and circuit interface Mar 15, 2023
@natestemen natestemen removed the needs/agreed-design Needs a plan of action that is agreed upon to complete. label Mar 15, 2023
@natestemen natestemen removed this from the 0.25.0 milestone Apr 1, 2023
@Aaron-Robertson
Copy link
Contributor

I'd like to pick this one up, but if anyone wants to collaborate I'm certainly happy to get some new folks involved with Mitiq as well!

@Misty-W
Copy link
Contributor

Misty-W commented May 26, 2023

Tagging @unitaryfund/engineering .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A request for a feature, tool, or workflow in Mitiq. interface-and-conversions How Mitiq interfaces with quantum software packages. unitaryhack-bounty
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants