# 需要导入模块: import sympy [as 别名]
# 或者: from sympy import sign [as 别名]
def cphase_to_sqrt_iswap(a, b, turns):
"""Implement a C-Phase gate using two sqrt ISWAP gates and single-qubit
operations. The circuit is equivalent to cirq.CZPowGate(exponent=turns).
Output unitary:
[1 0 0 0],
[0 1 0 0],
[0 0 1 0],
[0 0 0 e^{i turns pi}].
Args:
a: the first qubit
b: the second qubit
turns: Exponent specifying the evolution time in number of rotations.
"""
theta = (turns % 2) * np.pi
if 0 <= theta <= np.pi:
sign = 1.
theta_prime = theta
elif np.pi < theta < 2 * np.pi:
sign = -1.
theta_prime = 2 * np.pi - theta
if np.isclose(theta, np.pi):
# If we are close to pi, just set values manually to avoid possible
# numerical errors with arcsin of greater than 1.0 (Ahem, Windows).
phi = np.pi / 2
xi = np.pi / 2
else:
phi = np.arcsin(np.sqrt(2) * np.sin(theta_prime / 4))
xi = np.arctan(np.tan(phi) / np.sqrt(2))
yield ops.rz(sign * 0.5 * theta_prime).on(a)
yield ops.rz(sign * 0.5 * theta_prime).on(b)
yield ops.rx(xi).on(a)
yield ops.X(b)**(-sign * 0.5)
yield SQRT_ISWAP_INV(a, b)
yield ops.rx(-2 * phi).on(a)
yield SQRT_ISWAP(a, b)
yield ops.rx(xi).on(a)
yield ops.X(b)**(sign * 0.5)
# Corrects global phase
yield ops.GlobalPhaseOperation(np.exp(sign * theta_prime * 0.25j))