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 library of gates with their equivalent circuit implementations. #3946

Merged
merged 6 commits into from
Mar 13, 2020

Conversation

kdk
Copy link
Member

@kdk kdk commented Mar 9, 2020

Summary

Adds EquivalenceLibrary structure from Qiskit/RFCs#6 .

Details and comments

@kdk kdk added this to To do in Circuits via automation Mar 9, 2020
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.

it looks good

"""Create a new equivalence library.

Args:
base - Base equivalence library to will be referenced if an entry is
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
base - Base equivalence library to will be referenced if an entry is
base - Base equivalence library to which will be referenced if an entry is

Comment on lines 94 to 96
self.assertTrue(
(entry[0] == first_equiv and entry[1] == second_equiv)
or (entry[1] == first_equiv and entry[0] == second_equiv))
Copy link
Member

Choose a reason for hiding this comment

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

maybe compare sets?

Suggested change
self.assertTrue(
(entry[0] == first_equiv and entry[1] == second_equiv)
or (entry[1] == first_equiv and entry[0] == second_equiv))
self.assertEqual(set(entry), set([first_equiv, second_equiv])

Why not return them in order?

Copy link
Member Author

Choose a reason for hiding this comment

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

Was on the fence about what the ordering should be. Ideas? (Insertion order oldest to newest from self to base is what we'll get now, I think.)

Copy link
Member

Choose a reason for hiding this comment

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

my original comments was for being independent of the order. But I see value in keeping some deterministic order. That might be a proto-way to define priorities. I think entry[0] == newest... maybe a stack?

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 would've preferred a set as well (for the return type and the assertion) but QuantumCircuits aren't hashable (as they're mutable). Agree deterministic order is good, but the caller should inspect the returned circuit to determine priority, so the exact order shouldn't be so important. I'll document the current order and remove the ors in the test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 7378cec .

@1ucian0
Copy link
Member

1ucian0 commented Mar 9, 2020

I'm not fully sure if this works or should work...

        eq_lib = EquivalenceLibrary()

        theta = Parameter('theta')
        phi = Parameter('phi')

        gate = OneQubitTwoParamGate(theta, phi)  
        equiv1 = QuantumCircuit(1)
        equiv1.u2(theta, phi, 0)                             # theta,phi

        eq_lib.add_equivalence(gate, equiv1)

        equiv2 = QuantumCircuit(1)
        equiv2.u2(phi, theta, 0)                            # phi,theta
        eq_lib.add_equivalence(gate, equiv2)

@kdk
Copy link
Member Author

kdk commented Mar 10, 2020

I'm not fully sure if this works or should work...

...

This will work now, but maybe the behavior isn't obvious. What would you expect in this case?

>>> eq_lib = EquivalenceLibrary()
>>> theta = Parameter('theta')
>>> phi = Parameter('phi')
>>> gate = OneQubitTwoParamGate(theta, phi)
>>> equiv1 = QuantumCircuit(1)
>>> equiv1.u2(theta, phi, 0)
>>> eq_lib.add_equivalence(gate, equiv1)
>>> equiv2 = QuantumCircuit(1)
>>> equiv2.u2(phi, theta, 0)
>>> eq_lib.add_equivalence(gate, equiv2)
>>> alpha = Parameter('alpha')
>>> beta = Parameter('beta')
>>> gate_query = OneQubitTwoParamGate(alpha, beta)
>>> entry = eq_lib.get_entry(gate_query)
>>> for circ in entry:
...   print(circ)
...
        ┌────────────────┐
q_0: |0>┤ U2(alpha,beta) ├
        └────────────────┘
        ┌────────────────┐
q_0: |0>┤ U2(beta,alpha) ├
        └────────────────┘

The above is saying that the gate is arg symmetric (both U2(a,b) and U2(b,a) implement 1q2p(a,b)). I can make it clearer in the docs that the mapping between the parameters of the gate and the paramters in the circuit is what is preserved in the library (and what will be reflected between the query gate and the returned circuits.)

@kdk kdk force-pushed the add-equivalence-library branch 2 times, most recently from 62869fc to 76fc079 Compare March 11, 2020 14:33
@ajavadia
Copy link
Member

mapping between the parameters of the gate and the paramters in the circuit is what is preserved in the library (and what will be reflected between the query gate and the returned circuits.)

yes this sounds like a good behavior.

@kdk kdk added this to the 0.13 milestone Mar 12, 2020
@kdk kdk added the automerge label Mar 13, 2020
@mergify mergify bot merged commit 9fe60cb into Qiskit:master Mar 13, 2020
Circuits automation moved this from To do to Done Mar 13, 2020
@mtreinish mtreinish added the Changelog: New Feature Include in the "Added" section of the changelog label Apr 9, 2020
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this pull request Aug 5, 2020
…iskit#3946)

* Add library of gates with their equivalent circuit implementations.

* Document equivalent circuit return order.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
Circuits
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants