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

Feature/convert more controlled gates #50

Merged
merged 11 commits into from
Jan 10, 2023

Conversation

CalMacCQ
Copy link
Contributor

@CalMacCQ CalMacCQ commented Dec 23, 2022

Redo of #49 avoiding annoying auto-formatting issues.

I'm adding support for general multi-controlled gates in the qiskit converters.

There is a case where the CircBox underlying the QControlBox has multiple qubits (e.g. when the base gate is an RZZGate) which I'm not quite sure I got right. I wouldn't mind discussing this further.
(edit: resolved)

Requested a review from Alec as well as the QControlBox was his suggestion.

Changes

  1. Fixed handling of CnRy gates:
    These used to be supported directly in tk_to_qiskit but after Conversion rebase #10 these were decomposed by the rebase prior to conversion. Adding the CnRy gate to the target gateset (_protected_tket_gates) should take care of this.
  2. Added support for CnY and CnZ gates:
    Add CnY and CnZ gates to converters #11. I previously thought this would be annoying to add - turns out its easy.
  3. Support other controlled gates using QControlBox:
    Add support for general controlled gates in converters #28. I've added the additional detail of naming the Circuit in the QControlBox to reflect the gate that is implemented (see image) instead of using the default "Circuit" name. Let me know if you think this is a good idea. Alternatively I could just call it "U" or something.

Screenshot 2022-12-23 at 17 40 28

Also some suggestions...

  1. We frequently use conditions like if type(i) == ControlledGate in this codebase. Shouldn't we be using if isinstance(i, ControlledGate) instead? Seems pylint recommends this too.
    qiskit_convert.py:299:17: C0123: Use isinstance() rather than type() for a typecheck. (unidiomatic-typecheck)
  2. The function _tk_gate_set seems to be unused... I assume we can remove this? ( edit: its used in another file)

Copy link
Collaborator

@cqc-alec cqc-alec left a comment

Choose a reason for hiding this comment

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

Just a couple of small suggestions.

"qiskit ControlledGate with "
+ "base gate {} not implemented".format(i.base_gate)
)
if type(i.base_gate) in set(_known_qiskit_gate.keys()):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if type(i.base_gate) in set(_known_qiskit_gate.keys()):
if type(i.base_gate) in _known_qiskit_gate:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done.

@@ -500,8 +518,11 @@ def append_tk_command_to_qiskit(
qargs = [qregmap[q.reg_name][q.index[0]] for q in args]
if optype == OpType.CnX:
return qcirc.mcx(qargs[:-1], qargs[-1])

# special case
num_controls = len(qargs) - 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think assuming that the "target" gate will always be single-qubit is a bug waiting to happen: better to set this in the individual cases below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, do you mean like this?

Screenshot 2023-01-03 at 10 21 29

Copy link
Contributor Author

@CalMacCQ CalMacCQ Jan 3, 2023

Choose a reason for hiding this comment

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

In fact I could just do away with the num_controls variable and just do

    if optype == OpType.CnY:
        return qcirc.append(qiskit_gates.YGate().control(len(qargs) - 1), qargs)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that would be better I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, done.

sub_circ = Circuit(n_base_qubits)
# use base gate name for the CircBox (shows in renderer)
sub_circ.name = instr.base_gate.name.capitalize()
sub_circ.add_gate(base_tket_gate, params, list(range(n_base_qubits)))
Copy link
Contributor Author

@CalMacCQ CalMacCQ Jan 3, 2023

Choose a reason for hiding this comment

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

Do you think that adding the base_tket_gate to the list given by list(range(n_base_qubits) is correct here? I guess I have implicitly assumed that the base gate acts on the base qubits symmetrically.

i.e. sub_circ.add_gate(base_tket_gate, params, [0, 1])) is equivalent to sub_circ.add_gate(base_tket_gate, params, [1, 0 ]))

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes I think it is correct. The order of qubits is handled below.

cqc-alec
cqc-alec previously approved these changes Jan 3, 2023
docs/changelog.rst Outdated Show resolved Hide resolved
Co-authored-by: Alec Edgington <54802828+cqc-alec@users.noreply.github.com>
cqc-alec
cqc-alec previously approved these changes Jan 3, 2023
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.

3 participants