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

Add support for optimized multi-controlled gates #124

Merged
merged 6 commits into from
Apr 12, 2019

Conversation

chriseclectic
Copy link
Member

@chriseclectic chriseclectic commented Apr 4, 2019

Summary

Adds support for multi-controller x,y,z,u1,u2,u3 gates to the statevector QasmSimulator, StatevectorSimulator and UnitarySimulator.

These gates have an optimized implementation making them faster to execute than the gate being unrolled into a basis of cx and single qubit gates.

Closes #127

Details and comments

These gates are passed in as (N+1)-qubit qobj instructions such as:

{"name": "mcx", "qubits": [c0, c1,...,cn, target]}

where the first N qubits are the controls, and the last qubit is the target.

For the controlled u1,u2,u3 gates the instruction includes the same parameters as for the single qubit gates. Eg:

{"name": "mcu3", "qubits": [c0, c1,...,cn, target], "params": [theta, phi, lam]}

TODO:

  • Add tests
  • Update Changelog

@chriseclectic chriseclectic added this to the 0.2 milestone Apr 4, 2019
@chriseclectic chriseclectic requested a review from atilag as a code owner April 4, 2019 22:13
@chriseclectic chriseclectic requested a review from ajavadia April 5, 2019 14:17
Copy link
Member

@ajavadia ajavadia left a comment

Choose a reason for hiding this comment

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

This looks good. Can you add a few tests? Not sure if you do your tests with manual Qobj or with Terra. If Terra, then we need to wait until Terra can generate these qobj instructions.
Edit: actually, you can already define any gate you want in terra.

Is it possible to have cswap (Fredking Gate), as well as mcswap? Not blocking for this PR.

// Calculate the permutation positions for the last qubit.
const size_t N = qubits.size();
const size_t pos0 = MASKS[N - 1];
const size_t pos1 = MASKS[N];
// Lambda function for CNOT gate
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Lambda function for CNOT gate
// Lambda function for mcx gate

const size_t pos0 = MASKS[N - 1];
const size_t pos1 = MASKS[N];
const complex_t I(0., 1.);
// Lambda function for CNOT gate
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Lambda function for CNOT gate
// Lambda function for mcy gate

const size_t N = qubits.size();
const size_t pos0 = MASKS[N - 1];
const size_t pos1 = MASKS[N];
// Lambda function for CNOT gate
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Lambda function for CNOT gate
// Lambda function for mcu gate

@chriseclectic
Copy link
Member Author

@ajavadia shouldnt be too hard to add a cswap and mcswap, I'll do it in a follow up PR though

atilag
atilag previously approved these changes Apr 10, 2019
@atilag
Copy link
Member

atilag commented Apr 10, 2019

@chriseclectic looks good to me!
Following Ali's recommendations, it'd be great to have tests and update the changelog accordingly.
I have added two tasks to the description to follow this.
Thanks!

@chriseclectic
Copy link
Member Author

The application of these functions for regular cx, ccx, cz, swap are already covered in current tests, but I want to wait till terra has a stable way of adding custom gates before adding additional tests, (this is tracked in issue #132).

@chriseclectic chriseclectic merged commit dd455f9 into Qiskit:master Apr 12, 2019
@chriseclectic chriseclectic deleted the optimized-gates branch April 30, 2019 20:12
@we-taper
Copy link

Any one know how to use this multiple controlled gates inside qiskit python?

dcmckayibm pushed a commit to dcmckayibm/qiskit-aer that referenced this pull request Nov 3, 2019
* add multi-controlled pauli gates

* implement cx, cz as mcx, mcz

* add multi controlled u1,u2,u3 gates

* fixup indexes refs

* update changelog
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.

Add multi-controlled single qubit gates
4 participants