def test_gate_from_info(): assert (_rotations._ROTATION_GATE_FROM_INFO["X"](cmath.pi / 4) == gates.Rx( cmath.pi / 4)) assert (_rotations._ROTATION_GATE_FROM_INFO["Y"](cmath.pi / 2) == gates.Ry( cmath.pi / 2)) assert (_rotations._ROTATION_GATE_FROM_INFO["Z"](cmath.pi) == gates.Rz( cmath.pi)) return
def visit_R(self, node, params): # type parameter t = params[0] typ = round(float(t)) # other params angle = params[2] q = params[4] # different angle convention angle = -angle # differentiate with type if typ == 1: return (ops.Rx(angle), (q, )) elif typ == 2: return (ops.Ry(angle), (q, )) elif typ == 3: return (ops.Rz(angle), (q, )) else: raise InvalidRotationGateError
def test_gate_to_info(): gate = gates.Rx(cmath.pi / 4) info = _rotations._ROTATION_GATE_TO_INFO[type(gate)](gate) assert (info[:2] == [[(0, "X")], "pi4"]) assert (abs(info[2] - (cmath.pi / 4)) < _PRECISION) gate = gates.Ry(cmath.pi / 2) info = _rotations._ROTATION_GATE_TO_INFO[type(gate)](gate) assert (info[:2] == [[(0, "Y")], "pi2"]) assert (abs(info[2] - (cmath.pi / 2)) < _PRECISION) gate = gates.Rz(cmath.pi) info = _rotations._ROTATION_GATE_TO_INFO[type(gate)](gate) assert (info[:2] == [[(0, "Z")], "pi"]) assert (abs(info[2] - (cmath.pi)) < _PRECISION) gate = gates.TimeEvolution(-cmath.pi / 8, gates.QubitOperator("X0 X1 Y2")) info = _rotations._ROTATION_GATE_TO_INFO[type(gate)](gate) assert (info[:2] == [[(0, "X"), (1, "X"), (2, "Y")], "pi4"]) assert (abs(info[2] - (cmath.pi / 4)) < _PRECISION) return
def test_Ry(benchmark, nbit): run_bench(benchmark, ops.Ry(0.5), 2, nbit)
_ROTATION_GATE_TO_INFO = { gates.XGate: lambda gate: [[(0,"X")], "pi", cmath.pi], gates.YGate: lambda gate: [[(0,"Y")], "pi", cmath.pi], gates.ZGate: lambda gate: [[(0,"Z")], "pi", cmath.pi], gates.SGate: lambda gate: [[(0,"Z")], "pi2", cmath.pi/2], gates.TGate: lambda gate: [[(0,"Z")], "pi4", cmath.pi/4], gates.Rx: lambda gate: [[(0,"X")]] + determine_rotation(gate), gates.Ry: lambda gate: [[(0,"Y")]] + determine_rotation(gate), gates.Rz: lambda gate: [[(0,"Z")]] + determine_rotation(gate), gates.TimeEvolution: lambda gate: time_evolution_info(gate), } _ROTATION_GATE_FROM_INFO = { "X" : lambda angle: gates.Rx(angle), "Y" : lambda angle: gates.Ry(angle), "Z" : lambda angle: gates.Rz(angle) } # # This lookup table implemets the following commutation relations relation # (1) P * P'(phi) = P'(phi) * P # (2) P'(phi) * P = P * P'(-phi) # (3) P(pi/2) * P'(phi) = (i P P')(phi) * P(pi/2) # (4) P'(phi) * P(pi/2) = P(pi/2) * (i P P')(-phi) # # Here, P and P' are bases for one of the Pauli-rotations (Rx, Ry, Rz). # The angle pi/2 indicates S gates, phi is arbitrary and if the angle is omitted, # a standard Pauli operator (angle = pi) is used.