From 3f93d55eaa12ea6bcb295b52231c108a1759ab84 Mon Sep 17 00:00:00 2001 From: ylin <17835344407@163.com> Date: Thu, 21 Dec 2023 15:33:23 +0800 Subject: [PATCH] feat: merge main --- quafu/dagcircuits/circuit_dag.py | 8 ++--- quafu/elements/element_gates/c11.py | 15 ++++---- quafu/elements/element_gates/c12.py | 8 +++-- quafu/elements/element_gates/clifford.py | 11 +++--- quafu/elements/element_gates/cm1.py | 13 +++---- quafu/elements/element_gates/pauli.py | 45 ++++++++++++++---------- quafu/elements/element_gates/swap.py | 9 ++--- quafu/elements/oracle.py | 9 ++--- quafu/visualisation/bloch_sphere.py | 25 ++++++++----- 9 files changed, 78 insertions(+), 65 deletions(-) diff --git a/quafu/dagcircuits/circuit_dag.py b/quafu/dagcircuits/circuit_dag.py index 7d891fc..dbdfea7 100644 --- a/quafu/dagcircuits/circuit_dag.py +++ b/quafu/dagcircuits/circuit_dag.py @@ -2,12 +2,12 @@ from typing import Any, List import networkx as nx -from quafu.dagcircuits.dag_circuit import ( +from quafu.dagcircuits.dag_circuit import ( # dag_circuit.py in the same folder as circuit_dag.py now DAGCircuit, -) # dag_circuit.py in the same folder as circuit_dag.py now -from quafu.dagcircuits.instruction_node import ( +) +from quafu.dagcircuits.instruction_node import ( # instruction_node.py in the same folder as circuit_dag.py now InstructionNode, -) # instruction_node.py in the same folder as circuit_dag.py now +) from quafu.elements import Barrier, Delay, Measure, XYResonance from quafu.elements.element_gates import * from quafu.elements.pulses import FlattopPulse, GaussianPulse, RectPulse diff --git a/quafu/elements/element_gates/c11.py b/quafu/elements/element_gates/c11.py index fa42e06..fbaf727 100644 --- a/quafu/elements/element_gates/c11.py +++ b/quafu/elements/element_gates/c11.py @@ -1,4 +1,3 @@ -from ..quantum_gate import ControlledGate, FixedGate, QuantumGate from abc import ABC from typing import Dict @@ -13,7 +12,7 @@ class _C11Gate(ControlledGate, ABC): ct_dims = (1, 1, 2) -@QuantumGate.register('cx') +@QuantumGate.register("cx") class CXGate(_C11Gate, FixedGate): name = "CX" @@ -22,7 +21,7 @@ def __init__(self, ctrl: int, targ: int): self.symbol = "+" -@QuantumGate.register('cy') +@QuantumGate.register("cy") class CYGate(_C11Gate, FixedGate): name = "CY" @@ -30,7 +29,7 @@ def __init__(self, ctrl: int, targ: int): _C11Gate.__init__(self, "Y", [ctrl], [targ], None, tar_matrix=YMatrix) -@QuantumGate.register('cz') +@QuantumGate.register("cz") class CZGate(_C11Gate, FixedGate): name = "CZ" @@ -38,7 +37,7 @@ def __init__(self, ctrl: int, targ: int): _C11Gate.__init__(self, "Z", [ctrl], [targ], None, tar_matrix=ZMatrix) -@QuantumGate.register('cs') +@QuantumGate.register("cs") class CSGate(_C11Gate, FixedGate): name = "CS" @@ -49,7 +48,7 @@ def to_qasm(self): return "cp(pi/2) " + "q[%d],q[%d]" % (self.pos[0], self.pos[1]) -@QuantumGate.register('ct') +@QuantumGate.register("ct") class CTGate(_C11Gate, FixedGate): name = "CT" @@ -60,7 +59,7 @@ def to_qasm(self): return "cp(pi/4) " + "q[%d],q[%d]" % (self.pos[0], self.pos[1]) -@QuantumGate.register('cp') +@QuantumGate.register("cp") class CPGate(_C11Gate): name = "CP" @@ -69,4 +68,4 @@ def __init__(self, ctrl: int, targ: int, paras): @property def named_paras(self) -> Dict: - return {'theta': self.paras} + return {"theta": self.paras} diff --git a/quafu/elements/element_gates/c12.py b/quafu/elements/element_gates/c12.py index e8cbfef..41f0ed7 100644 --- a/quafu/elements/element_gates/c12.py +++ b/quafu/elements/element_gates/c12.py @@ -1,11 +1,13 @@ -from ..quantum_gate import ControlledGate, FixedGate, QuantumGate from quafu.elements.matrices import SwapMatrix from ..quantum_gate import ControlledGate, FixedGate, QuantumGate -@QuantumGate.register('cswap') + +@QuantumGate.register("cswap") class FredkinGate(ControlledGate, FixedGate): name = "CSWAP" def __init__(self, ctrl: int, targ1: int, targ2: int): - ControlledGate.__init__(self, "SWAP", [ctrl], [targ1, targ2], None, tar_matrix=SwapMatrix) + ControlledGate.__init__( + self, "SWAP", [ctrl], [targ1, targ2], None, tar_matrix=SwapMatrix + ) diff --git a/quafu/elements/element_gates/clifford.py b/quafu/elements/element_gates/clifford.py index 0529a9f..51a9bb0 100644 --- a/quafu/elements/element_gates/clifford.py +++ b/quafu/elements/element_gates/clifford.py @@ -1,13 +1,12 @@ import numpy as np from quafu.elements.matrices import HMatrix, SMatrix -from ..quantum_gate import SingleQubitGate, FixedGate, QuantumGate from ..quantum_gate import FixedGate, QuantumGate, SingleQubitGate __all__ = ["HGate", "SGate", "SdgGate", "TGate", "TdgGate"] -@QuantumGate.register('h') +@QuantumGate.register("h") class HGate(SingleQubitGate, FixedGate): name = "H" matrix = HMatrix @@ -16,7 +15,7 @@ def __init__(self, pos: int): FixedGate.__init__(self, pos) -@QuantumGate.register('s') +@QuantumGate.register("s") class SGate(SingleQubitGate, FixedGate): name = "S" matrix = SMatrix @@ -25,7 +24,7 @@ def __init__(self, pos: int): FixedGate.__init__(self, pos) -@QuantumGate.register('sdg') +@QuantumGate.register("sdg") class SdgGate(SingleQubitGate, FixedGate): name = "Sdg" matrix = SMatrix.conj().T @@ -34,7 +33,7 @@ def __init__(self, pos: int): FixedGate.__init__(self, pos) -@QuantumGate.register('t') +@QuantumGate.register("t") class TGate(SingleQubitGate, FixedGate): name = "T" matrix = np.array([[1.0, 0.0], [0.0, np.exp(1.0j * np.pi / 4)]], dtype=complex) @@ -43,7 +42,7 @@ def __init__(self, pos: int): FixedGate.__init__(self, pos) -@QuantumGate.register('tdg') +@QuantumGate.register("tdg") class TdgGate(SingleQubitGate, FixedGate): name = "Tdg" matrix = TGate.matrix.conj().T diff --git a/quafu/elements/element_gates/cm1.py b/quafu/elements/element_gates/cm1.py index b9885ca..f041c4d 100644 --- a/quafu/elements/element_gates/cm1.py +++ b/quafu/elements/element_gates/cm1.py @@ -1,4 +1,3 @@ -from ..quantum_gate import ControlledGate, FixedGate, QuantumGate from quafu.elements.matrices import XMatrix, YMatrix, ZMatrix from ..quantum_gate import ControlledGate, FixedGate, QuantumGate @@ -6,7 +5,7 @@ __all__ = ["MCXGate", "MCYGate", "MCZGate", "ToffoliGate"] -@QuantumGate.register('mcx') +@QuantumGate.register("mcx") class MCXGate(ControlledGate, FixedGate): name = "MCX" @@ -14,7 +13,7 @@ def __init__(self, ctrls, targ: int): ControlledGate.__init__(self, "X", ctrls, [targ], None, tar_matrix=XMatrix) -@QuantumGate.register('mcy') +@QuantumGate.register("mcy") class MCYGate(ControlledGate, FixedGate): name = "MCY" @@ -22,7 +21,7 @@ def __init__(self, ctrls, targ: int): ControlledGate.__init__(self, "Y", ctrls, [targ], None, tar_matrix=YMatrix) -@QuantumGate.register('mcz') +@QuantumGate.register("mcz") class MCZGate(ControlledGate, FixedGate): name = "MCZ" @@ -30,9 +29,11 @@ def __init__(self, ctrls, targ: int): ControlledGate.__init__(self, "Z", ctrls, [targ], None, tar_matrix=ZMatrix) -@QuantumGate.register('ccx') +@QuantumGate.register("ccx") class ToffoliGate(ControlledGate, FixedGate): name = "CCX" def __init__(self, ctrl1: int, ctrl2: int, targ: int): - ControlledGate.__init__(self, "X", [ctrl1, ctrl2], [targ], None, tar_matrix=XMatrix) + ControlledGate.__init__( + self, "X", [ctrl1, ctrl2], [targ], None, tar_matrix=XMatrix + ) diff --git a/quafu/elements/element_gates/pauli.py b/quafu/elements/element_gates/pauli.py index 6295656..a5ed783 100644 --- a/quafu/elements/element_gates/pauli.py +++ b/quafu/elements/element_gates/pauli.py @@ -2,15 +2,22 @@ from quafu.elements.matrices import SWMatrix, WMatrix, XMatrix, YMatrix, ZMatrix from quafu.elements.quantum_gate import FixedGate, QuantumGate, SingleQubitGate -from quafu.elements.matrices import XMatrix, YMatrix, ZMatrix, WMatrix, SWMatrix -from quafu.elements.quantum_gate import FixedGate, SingleQubitGate, QuantumGate - -__all__ = ['IdGate', 'XGate', 'YGate', 'ZGate', - 'WGate', 'SWGate', 'SWdgGate', - 'SXGate', 'SYGate', 'SXdgGate', 'SYdgGate'] # hint: "SZ" gate is S contained in Clifford gates - - -@QuantumGate.register('id') +__all__ = [ + "IdGate", + "XGate", + "YGate", + "ZGate", + "WGate", + "SWGate", + "SWdgGate", + "SXGate", + "SYGate", + "SXdgGate", + "SYdgGate", +] # hint: "SZ" gate is S contained in Clifford gates + + +@QuantumGate.register("id") class IdGate(FixedGate, SingleQubitGate): name = "Id" matrix = XMatrix @@ -19,7 +26,7 @@ def __init__(self, pos: int): FixedGate.__init__(self, pos) -@QuantumGate.register('x') +@QuantumGate.register("x") class XGate(FixedGate, SingleQubitGate): name = "X" matrix = XMatrix @@ -28,7 +35,7 @@ def __init__(self, pos: int): FixedGate.__init__(self, pos) -@QuantumGate.register('y') +@QuantumGate.register("y") class YGate(FixedGate, SingleQubitGate): name = "Y" matrix = YMatrix @@ -37,7 +44,7 @@ def __init__(self, pos: int): FixedGate.__init__(self, pos) -@QuantumGate.register('z') +@QuantumGate.register("z") class ZGate(FixedGate, SingleQubitGate): name = "Z" matrix = ZMatrix @@ -46,7 +53,7 @@ def __init__(self, pos: int): FixedGate.__init__(self, pos) -@QuantumGate.register('w') +@QuantumGate.register("w") class WGate(FixedGate, SingleQubitGate): """(X+Y)/sqrt(2)""" @@ -65,7 +72,7 @@ def to_qasm(self): ) -@QuantumGate.register('sw') +@QuantumGate.register("sw") class SWGate(FixedGate, SingleQubitGate): name = "SW" matrix = SWMatrix @@ -82,7 +89,7 @@ def to_qasm(self): ) -@QuantumGate.register('swdg') +@QuantumGate.register("swdg") class SWdgGate(FixedGate, SingleQubitGate): name = "SWdg" matrix = SWMatrix @@ -99,7 +106,7 @@ def to_qasm(self): ) -@QuantumGate.register('sx') +@QuantumGate.register("sx") class SXGate(FixedGate, SingleQubitGate): name = "SX" matrix = np.array( @@ -110,7 +117,7 @@ def __init__(self, pos: int): FixedGate.__init__(self, pos) -@QuantumGate.register('sxdg') +@QuantumGate.register("sxdg") class SXdgGate(FixedGate, SingleQubitGate): name = "SXdg" matrix = SXGate.matrix.conj().T @@ -120,7 +127,7 @@ def __init__(self, pos: int): self.symbol = "√X†" -@QuantumGate.register('sy') +@QuantumGate.register("sy") class SYGate(FixedGate, SingleQubitGate): name = "SY" matrix = np.array( @@ -135,7 +142,7 @@ def to_qasm(self): return "ry(pi/2) q[%d];" % self.pos -@QuantumGate.register('sydg') +@QuantumGate.register("sydg") class SYdgGate(FixedGate, SingleQubitGate): name = "SYdg" matrix = SYGate.matrix.conj().T diff --git a/quafu/elements/element_gates/swap.py b/quafu/elements/element_gates/swap.py index 4667056..acbf447 100644 --- a/quafu/elements/element_gates/swap.py +++ b/quafu/elements/element_gates/swap.py @@ -1,6 +1,3 @@ -from ..quantum_gate import FixedGate, MultiQubitGate, QuantumGate -from quafu.elements.matrices import ISwapMatrix, SwapMatrix - from typing import Dict from quafu.elements.matrices import ISwapMatrix, SwapMatrix @@ -10,7 +7,7 @@ __all__ = ["ISwapGate", "SwapGate"] -@QuantumGate.register('iswap') +@QuantumGate.register("iswap") class ISwapGate(FixedGate, MultiQubitGate): name = "iSWAP" matrix = ISwapMatrix @@ -27,7 +24,7 @@ def named_pos(self) -> Dict: return {"pos": self.pos} -@QuantumGate.register('swap') +@QuantumGate.register("swap") class SwapGate(FixedGate, MultiQubitGate): name = "SWAP" matrix = SwapMatrix @@ -41,4 +38,4 @@ def get_targ_matrix(self, reverse_order=False): @property def named_pos(self) -> Dict: - return {'pos': self.pos} + return {"pos": self.pos} diff --git a/quafu/elements/oracle.py b/quafu/elements/oracle.py index 50ba731..794eb3d 100644 --- a/quafu/elements/oracle.py +++ b/quafu/elements/oracle.py @@ -129,10 +129,11 @@ def customize_gate( if cls_name in QuantumGate.gate_classes: raise ValueError(f"Gate class {cls_name} already exists.") - attrs = {'cls_name': cls_name, - 'gate_structure': gate_structure, - 'qubit_num': qubit_num, - } + attrs = { + "cls_name": cls_name, + "gate_structure": gate_structure, + "qubit_num": qubit_num, + } customized_cls = OracleGateMeta(cls_name, (OracleGate,), attrs) assert issubclass(customized_cls, OracleGate) diff --git a/quafu/visualisation/bloch_sphere.py b/quafu/visualisation/bloch_sphere.py index c72676f..e19cc1a 100644 --- a/quafu/visualisation/bloch_sphere.py +++ b/quafu/visualisation/bloch_sphere.py @@ -28,7 +28,7 @@ def xyz_to_angles(xs, ys, zs): def hex_to_rgb(hex_color): """Transform a hex color code to RGB (normalized float).""" - hex_color = hex_color.lstrip('#') + hex_color = hex_color.lstrip("#") if len(hex_color) != 6: raise ValueError("Invalid hex color code") @@ -52,7 +52,7 @@ def plot_bloch_vector(v_x, v_y, v_z, title=""): ax: matplotlib axes of the Bloch sphere plot. """ fig = plt.figure() - ax = fig.add_subplot(111, projection='3d') + ax = fig.add_subplot(111, projection="3d") # surface of Bloch sphere theta = np.linspace(0, np.pi, 21) @@ -60,8 +60,8 @@ def plot_bloch_vector(v_x, v_y, v_z, title=""): theta, phi = np.meshgrid(theta, phi) x, y, z = angles_to_xyz(theta, phi) - surf = ax.plot_surface(x, y, z, color='white', alpha=0.2) - edge_color = hex_to_rgb('#000000') # #ff7f0e + surf = ax.plot_surface(x, y, z, color="white", alpha=0.2) + edge_color = hex_to_rgb("#000000") # #ff7f0e edge_alpha = 0.05 surf.set_edgecolor((edge_color[0], edge_color[1], edge_color[2], edge_alpha)) @@ -72,16 +72,23 @@ def plot_bloch_vector(v_x, v_y, v_z, title=""): ax.plot(0 * span, span, zs=0, zdir="y", label="Z", lw=1, color="black", alpha=0.5) # coordinate values - ax.text(1.4, 0, 0, 'x', color='black') - ax.text(0, 1.2, 0, 'y', color='black') - ax.text(0, 0, 1.2, 'z', color='black') + ax.text(1.4, 0, 0, "x", color="black") + ax.text(0, 1.2, 0, "y", color="black") + ax.text(0, 0, 1.2, "z", color="black") # Bloch vector - ax.quiver(0, 0, 0, v_x, v_y, v_z, color='r') + ax.quiver(0, 0, 0, v_x, v_y, v_z, color="r") v_theta, v_phi = xyz_to_angles(v_x, v_y, v_z) # coordinates value text - ax.text(0, 0, 1.6, 'Bloch vector: ($\\theta=${:.2f}, $\\varphi$={:.2f})'.format(v_theta, v_phi), fontsize=8, color='red') + ax.text( + 0, + 0, + 1.6, + "Bloch vector: ($\\theta=${:.2f}, $\\varphi$={:.2f})".format(v_theta, v_phi), + fontsize=8, + color="red", + ) # ax.text(0, 0, 1.6, 'Bloch vector: ({:.2f}, {:.2f}, {:.2f})'.format(v_x, v_y, v_z), fontsize=8, color='red') # Set the range of the axes