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

Allow users to specify cut decompositions in terms of a native gate set #495

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion circuit_knitting/cutting/qpd/qpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
from .instructions import BaseQPDGate, TwoQubitQPDGate, QPDMeasure
from ..instructions import Move
from ...utils.iteration import unique_by_id, strict_zip
from ...utils.equivalence import EagleEquivalenceLibrary


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -1143,7 +1144,13 @@ def _decompose_qpd_instructions(
for data in inst.operation.definition.data:
# Can ignore clbits here, as QPDGates don't use clbits directly
assert data.clbits == ()
tmp_data.append(CircuitInstruction(data.operation, qubits=[qubits[0]]))
equiv = EagleEquivalenceLibrary.get_entry(data.operation)[0]
if equiv == []:
tmp_data.append(CircuitInstruction(data.operation, qubits=[qubits[0]]))
else:
# CKT equivalence libraries only define one mapping per input
for d in equiv[0].data:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should assert equiv is length-1 here

tmp_data.append(CircuitInstruction(d.operation, qubits=[qubits[0]]))
# Replace QPDGate with local operations
if tmp_data:
# Overwrite the QPDGate with first instruction
Expand Down
6 changes: 6 additions & 0 deletions circuit_knitting/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@
===================================================================

.. automodule:: circuit_knitting.utils.transpiler_passes

===================================================================
Gate equivalence rules (:mod:`circuit_knitting.utils.equivalence`)
===================================================================

.. automodule:: circuit_knitting.utils.equivalence
"""
155 changes: 155 additions & 0 deletions circuit_knitting/utils/equivalence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# This code is a Qiskit project.

# (C) Copyright IBM 2024.

# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""
Equivalence utilities.

.. currentmodule:: circuit_knitting.utils.equivalence

.. autosummary::
:toctree: ../stubs/

"""

import numpy as np
from qiskit.circuit import (
EquivalenceLibrary,
QuantumCircuit,
QuantumRegister,
Parameter,
)
from qiskit.circuit.library.standard_gates import (
RZGate,
XGate,
YGate,
ZGate,
HGate,
SGate,
IGate,
SdgGate,
SXGate,
SXdgGate,
TGate,
TdgGate,
RXGate,
RYGate,
PhaseGate,
)

_eagle_sel = HeronEquivalenceLibrary = EagleEquivalenceLibrary = EquivalenceLibrary()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should document that these libraries are for single-qubit operations only. Should also document that it is assumed that each input will produce, at most, one equivalence.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe the names of the classes should include SingleQubitEagle...


########## Single-qubit Eagle native gate set: x, sx, rz, i ##########
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's better to delete the native gates from the equivalence library. Qiskit equivalence libraries don't return the gate itself, only alternate equivalences.

# XGate
q = QuantumRegister(1, "q")
def_x = QuantumCircuit(q)
def_x.append(XGate(), [0], [])
_eagle_sel.add_equivalence(XGate(), def_x)

# SXGate
q = QuantumRegister(1, "q")
def_sx = QuantumCircuit(q)
def_sx.append(SXGate(), [0], [])
_eagle_sel.add_equivalence(SXGate(), def_sx)

# RZGate
q = QuantumRegister(1, "q")
def_rz = QuantumCircuit(q)
theta = Parameter("theta")
def_rz.append(RZGate(theta), [0], [])
_eagle_sel.add_equivalence(RZGate(theta), def_rz)

# IGate
q = QuantumRegister(1, "q")
def_i = QuantumCircuit(q)
def_i.append(IGate(), [0], [])
_eagle_sel.add_equivalence(IGate(), def_i)

######################################################################

# YGate
q = QuantumRegister(1, "q")
def_y = QuantumCircuit(q)
for inst in [RZGate(np.pi), XGate()]:
def_y.append(inst, [0], [])
_eagle_sel.add_equivalence(YGate(), def_y)

# ZGate
q = QuantumRegister(1, "q")
def_z = QuantumCircuit(q)
def_z.append(RZGate(np.pi), [0], [])
_eagle_sel.add_equivalence(ZGate(), def_z)

# HGate
q = QuantumRegister(1, "q")
def_h = QuantumCircuit(q)
for inst in [RZGate(np.pi / 2), SXGate(), RZGate(np.pi / 2)]:
def_h.append(inst, [0], [])
_eagle_sel.add_equivalence(HGate(), def_h)

# SGate
q = QuantumRegister(1, "q")
def_s = QuantumCircuit(q)
def_s.append(RZGate(np.pi / 2), [0], [])
_eagle_sel.add_equivalence(SGate(), def_s)

# SdgGate
q = QuantumRegister(1, "q")
def_sdg = QuantumCircuit(q)
def_sdg.append(RZGate(-np.pi / 2), [0], [])
_eagle_sel.add_equivalence(SdgGate(), def_sdg)

# SXdgGate
q = QuantumRegister(1, "q")
def_sxdg = QuantumCircuit(q)
for inst in [
RZGate(np.pi / 2),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The BasisTranslator returned this. Fairly certain we can collapse to one pi rotation on either side of the sxgate

RZGate(np.pi / 2),
SXGate(),
RZGate(np.pi / 2),
RZGate(np.pi / 2),
]:
def_sxdg.append(inst, [0], [])
_eagle_sel.add_equivalence(SXdgGate(), def_sxdg)

# TGate
q = QuantumRegister(1, "q")
def_t = QuantumCircuit(q)
def_t.append(RZGate(np.pi / 4), [0], [])
_eagle_sel.add_equivalence(TGate(), def_t)

# TdgGate
q = QuantumRegister(1, "q")
def_tdg = QuantumCircuit(q)
def_tdg.append(RZGate(-np.pi / 4), [0], [])
_eagle_sel.add_equivalence(TdgGate(), def_tdg)

# RXGate
q = QuantumRegister(1, "q")
def_rx = QuantumCircuit(q)
theta = Parameter("theta")
for inst in [RZGate(np.pi / 2), SXGate(), RZGate(theta + np.pi), RZGate(5 * np.pi / 2)]:
def_rx.append(inst, [0], [])
_eagle_sel.add_equivalence(RXGate(theta), def_rx)

# RYGate
q = QuantumRegister(1, "q")
def_ry = QuantumCircuit(q)
theta = Parameter("theta")
for inst in [SXGate(), RZGate(theta + np.pi), SXGate(), RZGate(3 * np.pi)]:
Copy link
Collaborator Author

@caleb-johnson caleb-johnson Feb 29, 2024

Choose a reason for hiding this comment

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

Another strange one from BasisTranslator I wasn't sure about. Can the 3*pi be simplified to just pi?

def_ry.append(inst, [0], [])
_eagle_sel.add_equivalence(RYGate(theta), def_ry)

# PhaseGate
q = QuantumRegister(1, "q")
def_p = QuantumCircuit(q)
theta = Parameter("theta")
def_p.append(RZGate(theta), [0], [])
_eagle_sel.add_equivalence(PhaseGate(theta), def_p)
Loading