def get_unitary(self, params: Sequence[float] = []) -> UnitaryMatrix: """Returns the unitary for this gate, see Unitary for more info.""" self.check_parameters(params) H = dot_product(params, self.sigmav) eiH = sp.linalg.expm(H) return UnitaryMatrix(eiH, check_arguments=False)
def test_full_pauli_gate() -> None: circuit = Circuit(3) circuit.append_gate(PauliGate(3), [0, 1, 2]) cost = HilbertSchmidtResiduals(circuit, UnitaryMatrix(unitary_group.rvs(8))) circuit.minimize(cost) assert cost.get_cost(circuit.get_params()) < 1e-6
def __init__( self, utry: UnitaryLike, radixes: Sequence[int] = [], ) -> None: self.utry = UnitaryMatrix(utry, radixes) self.size = self.utry.get_size() self.radixes = self.utry.get_radixes()
class YGate(ConstantGate, QubitGate): """The Pauli Y gate.""" size = 1 qasm_name = 'y' utry = UnitaryMatrix([ [0, -1j], [1j, 0], ], )
class XGate(ConstantGate, QubitGate): """The Pauli X gate.""" size = 1 qasm_name = 'x' utry = UnitaryMatrix([ [0, 1], [1, 0], ], )
class SGate(ConstantGate, QubitGate): """The S gate.""" size = 1 qasm_name = 's' utry = UnitaryMatrix([ [1, 0], [0, 1j], ], )
class ZGate(ConstantGate, QubitGate): """The Pauli Z gate.""" size = 1 qasm_name = 'z' utry = UnitaryMatrix([ [1, 0], [0, -1], ], )
class SqrtXGate(ConstantGate, QubitGate): """The Sqrt(X) gate.""" size = 1 qasm_name = 'sx' utry = UnitaryMatrix([ [np.sqrt(2) / 2, -1j * np.sqrt(2) / 2], [-1j * np.sqrt(2) / 2, np.sqrt(2) / 2], ], )
class TdgGate(ConstantGate, QubitGate): """The T Dagger gate.""" size = 1 qasm_name = 'tdg' utry = UnitaryMatrix([ [1, 0], [0, np.exp(-1j * np.pi / 4)], ], )
class TGate(ConstantGate, QubitGate): """The T gate.""" size = 1 qasm_name = 't' utry = UnitaryMatrix([ [1, 0], [0, np.exp(1j * np.pi / 4)], ], )
class HGate(ConstantGate, QubitGate): """The Hadamard gate.""" size = 1 qasm_name = 'h' utry = UnitaryMatrix([ [np.sqrt(2) / 2, np.sqrt(2) / 2], [np.sqrt(2) / 2, -np.sqrt(2) / 2], ], )
class SqrtCNOTGate(ConstantGate, QubitGate): size = 2 qasm_name = 'csx' utry = UnitaryMatrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0.5 + 0.5j, 0.5 - 0.5j], [0, 0, 0.5 - 0.5j, 0.5 + 0.5j], ], )
def get_unitary(self, params: Sequence[float] = []) -> UnitaryMatrix: """Returns the unitary for this gate, see Unitary for more info.""" self.check_parameters(params) if hasattr(self, 'utry'): return self.utry # TODO: Find reference U = self.gate.get_unitary(params) right = np.kron(self.OneProj, U) return UnitaryMatrix(self.left + right, self.get_radixes())
def get_unitary(self, params: Sequence[float] = []) -> UnitaryMatrix: """Returns the unitary for this gate, see Unitary for more info.""" self.check_parameters(params) exp = np.exp(1j * params[0]) return UnitaryMatrix([ [1, 0], [0, exp], ], )
def get_unitary_and_grad( self, params: Sequence[float] = [], ) -> tuple[UnitaryMatrix, np.ndarray]: """Returns the unitary and gradient, see Gate for more info.""" self.check_parameters(params) H = dot_product(params, self.sigmav) U, dU = dexpmv(H, self.sigmav) return UnitaryMatrix(U, check_arguments=False), dU
class SwapGate(ConstantGate, QubitGate): """The swap gate.""" size = 2 qasm_name = 'swap' utry = UnitaryMatrix([ [1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1], ], )
class SdgGate(ConstantGate, QubitGate): """The S Dagger gate.""" size = 1 qasm_name = 'sdg' utry = UnitaryMatrix( [ [1, 0], [0, -1j], ], )
class CNOTGate(ConstantGate, QubitGate): """The controlled-not or controlled-X gate.""" size = 2 qasm_name = 'cx' utry = UnitaryMatrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], ], )
def get_unitary(self, params: Sequence[float] = []) -> UnitaryMatrix: """Returns the unitary for this gate, see Unitary for more info.""" self.check_parameters(params) cos = np.cos(params[0] / 2) sin = -1j * np.sin(params[0] / 2) return UnitaryMatrix([ [cos, sin], [sin, cos], ], )
def test_minimize_ceres() -> None: circ = Circuit(1) circ.append_gate(RXGate(), location=[0], params=[0.0]) xgate = XGate() xutry = xgate.get_unitary() cost = HilbertSchmidtResidualsGenerator().gen_cost( circ, UnitaryMatrix(-1j * xutry.get_numpy()), ) minimizer = CeresMinimizer() x = minimizer.minimize(cost, np.array([np.pi / 2])) assert cost.get_cost(x) < 1e-6, x
class CHGate(ConstantGate, QubitGate): """The controlled-H gate.""" size = 2 qasm_name = 'ch' utry = UnitaryMatrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, np.sqrt(2) / 2, np.sqrt(2) / 2], [0, 0, np.sqrt(2) / 2, -np.sqrt(2) / 2], ], )
class XXGate(ConstantGate, QubitGate): """The Ising XX coupling gate.""" size = 2 qasm_name = 'rxx(pi/2)' utry = UnitaryMatrix([ [np.sqrt(2) / 2, 0, 0, -1j * np.sqrt(2) / 2], [0, np.sqrt(2) / 2, -1j * np.sqrt(2) / 2, 0], [0, -1j * np.sqrt(2) / 2, np.sqrt(2) / 2, 0], [-1j * np.sqrt(2) / 2, 0, 0, np.sqrt(2) / 2], ], )
def get_unitary(self, params: Sequence[float] = []) -> UnitaryMatrix: """Returns the unitary for this gate, see Unitary for more info.""" self.check_parameters(params) sq2 = np.sqrt(2) / 2 eip = np.exp(1j * params[0]) eil = np.exp(1j * params[1]) return UnitaryMatrix([ [sq2, -eil * sq2], [eip * sq2, eip * eil * sq2], ], )
class CZGate(ConstantGate, QubitGate): """The controlled-Z gate.""" size = 2 qasm_name = 'cz' utry = UnitaryMatrix( [ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1], ], )
def get_unitary(self, params: Sequence[float] = []) -> UnitaryMatrix: """Returns the unitary for this gate, see Unitary for more info.""" self.check_parameters(params) pos = np.exp(1j * params[0] / 2) neg = np.exp(-1j * params[0] / 2) return UnitaryMatrix([ [neg, 0, 0, 0], [0, pos, 0, 0], [0, 0, pos, 0], [0, 0, 0, neg], ], )
class CYGate(ConstantGate, QubitGate): """The controlled-Y gate.""" size = 2 qasm_name = 'cy' utry = UnitaryMatrix( [ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, -1j], [0, 0, 1j, 0], ], )
class CPIGate(ConstantGate, QutritGate): """The CPI gate.""" size = 2 utry = UnitaryMatrix([ [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1], ], )
class CSUMGate(ConstantGate, QutritGate): """The Conditional-SUM gate.""" size = 2 utry = UnitaryMatrix( [ [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1], ], )
def get_unitary(self, params: Sequence[float] = []) -> UnitaryMatrix: """Returns the unitary for this gate, see Unitary for more info.""" self.check_parameters(params) ct = np.cos(params[0] / 2) st = np.sin(params[0] / 2) cp = np.cos(params[1]) sp = np.sin(params[1]) cl = np.cos(params[2]) sl = np.sin(params[2]) el = cl + 1j * sl ep = cp + 1j * sp return UnitaryMatrix([ [ct, -el * st], [ep * st, ep * el * ct], ], )
def check_target( self, target: UnitaryLike | StateLike, ) -> UnitaryMatrix | StateVector: """Check `target` to be valid and return it casted.""" try: typed_target = StateVector(target) # type: ignore except (ValueError, TypeError): try: typed_target = UnitaryMatrix(target) # type: ignore except (ValueError, TypeError) as ex: raise TypeError( 'Expected either StateVector, UnitaryMatrix, or' ' CostFunction for target, got %s.' % type(target), ) from ex return typed_target