3

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 :)

Dara O h
  • 89
  • 8

2 Answers2

2

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')) 
cathulhu
  • 641
  • 1
  • 9
  • 20
2

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

Dara O h
  • 89
  • 8