如何在sympy.physics.quantum.gate Quater中创建受控的z门

发布于 2025-02-07 05:58:07 字数 1096 浏览 4 评论 0原文

我想知道是否可以使用Sympy.physics.quantum.gate软件包创建受控的Z-Gate?

我尝试了这样的事情

CGate(((0,1), Z(2)))

,其中​​第一个元组是控件,第二个元组是门,但是我得到了此错误

​在多个量子位上设置控件,也能够在任何门中掉落。

另外,是否可以创建一个arbitary旋转门并将其与此使用?例如,我在下面创建的arbitary x旋转门

def Rx(ctrl=1, angle=theta):
"""Rotation in x axis

Args:
    ctrl (int, optional): which qubit from bottom up to apply to. Defaults to 1.
    angle (_type_, optional): enter angle or leave as theta symbol. Defaults to theta.

Returns:
    rotation: set of gate rotations
"""
rot = cos(theta/2)*Im-j*sin(theta/2)*Xm
return UGate((ctrl,), rot.subs(theta, angle))

,其中xm = “

任何帮助都将不胜感激:)

I am wondering if it is possible to create a controlled Z-Gate using the sympy.physics.quantum.gate package?

enter image description here

I've tried something like this

CGate(((0,1), Z(2)))

where the first tuple are the controls and the second the gate but I'm getting this error

enter image description here

Ideally I'd like to be able to set controls on multiple qubits and to also be able to drop in any gate.

also, is it possible to to create an arbitary rotation gate and use it with this? eg the arbitary x rotation gate I created below

def Rx(ctrl=1, angle=theta):
"""Rotation in x axis

Args:
    ctrl (int, optional): which qubit from bottom up to apply to. Defaults to 1.
    angle (_type_, optional): enter angle or leave as theta symbol. Defaults to theta.

Returns:
    rotation: set of gate rotations
"""
rot = cos(theta/2)*Im-j*sin(theta/2)*Xm
return UGate((ctrl,), rot.subs(theta, angle))

where Xm =
![$X = \begin{bmatrix}
0& 1\
1& 0
\end{bmatrix} $

Any help would be really appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩起发的微风 2025-02-14 05:58:07

您创建多控制门的方法应起作用,您只需要删除过多的括号:

gate = CGate((0, 1), Z(2))
qapply(gate * Qubit('111')) 

Your approach to creating multi-controlled gates should work, you just need to remove excessive parentheses:

gate = CGate((0, 1), Z(2))
qapply(gate * Qubit('111')) 
大海や 2025-02-14 05:58:07

感谢@cathulhu 我认为以下将创建任意控制的x,y,z
具有反控制能力的大门。

def CX(controls: tuple, qubit: int, inverse: tuple =()) -> Gate:
    """Create a controlled X Gate

    Args:
        controls (tuple): control qubits
        qubit (int): affected qubit
        inverse (tuple, optional): controls to inverse. Defaults to None.

    Returns:
        Gate: Controlled X Gate
    """
    mygate = CGate(controls, X(qubit))
    
    for inv in inverse:
        mygate= X(inv)*mygate*X(inv)

    return mygate

def CY(controls: tuple, qubit: int, inverse: tuple =()) -> Gate:
    """Create a controlled Y Gate

    Args:
        controls (tuple): control qubits
        qubit (int): affected qubit
        inverse (tuple, optional): controls to inverse. Defaults to None.

    Returns:
        Gate: Controlled Y Gate
    """
    mygate = CGate(controls, Y(qubit))
    
    for inv in inverse:
        mygate= X(inv)*mygate*X(inv)

    return mygate

def CZ(controls: tuple, qubit: int, inverse: tuple =()) -> Gate:
    """Create a controlled Z Gate

    Args:
        controls (tuple): control qubits
        qubit (int): affected qubit
        inverse (tuple, optional): controls to inverse. Defaults to None.

    Returns:
        Gate: Controlled Z Gate
    """
    mygate = CGate(controls, Z(qubit))
    
    for inv in inverse:
        mygate= X(inv)*mygate*X(inv)

    return mygate

示例:

circuit_plot(CX((0,1),2,(1,)),3)

”在此处输入图像描述”

Thanks to @cathulhu I think the following will create arbitrary controlled X,Y,Z
gates with the ability to inverse controls.

def CX(controls: tuple, qubit: int, inverse: tuple =()) -> Gate:
    """Create a controlled X Gate

    Args:
        controls (tuple): control qubits
        qubit (int): affected qubit
        inverse (tuple, optional): controls to inverse. Defaults to None.

    Returns:
        Gate: Controlled X Gate
    """
    mygate = CGate(controls, X(qubit))
    
    for inv in inverse:
        mygate= X(inv)*mygate*X(inv)

    return mygate

def CY(controls: tuple, qubit: int, inverse: tuple =()) -> Gate:
    """Create a controlled Y Gate

    Args:
        controls (tuple): control qubits
        qubit (int): affected qubit
        inverse (tuple, optional): controls to inverse. Defaults to None.

    Returns:
        Gate: Controlled Y Gate
    """
    mygate = CGate(controls, Y(qubit))
    
    for inv in inverse:
        mygate= X(inv)*mygate*X(inv)

    return mygate

def CZ(controls: tuple, qubit: int, inverse: tuple =()) -> Gate:
    """Create a controlled Z Gate

    Args:
        controls (tuple): control qubits
        qubit (int): affected qubit
        inverse (tuple, optional): controls to inverse. Defaults to None.

    Returns:
        Gate: Controlled Z Gate
    """
    mygate = CGate(controls, Z(qubit))
    
    for inv in inverse:
        mygate= X(inv)*mygate*X(inv)

    return mygate

Example:

circuit_plot(CX((0,1),2,(1,)),3)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文