def phasegate(theta): """ Returns quantum object representing the phase shift gate. Parameters ---------- theta : float Phase rotation angle. Returns ------- phase_gate : qobj Quantum object representation of phase shift gate. Examples -------- >>> phasegate(pi/4) Quantum object: dims = [[2], [2]], shape = [2, 2], type = oper, isHerm = False Qobj data = [[ 1.00000000+0.j 0.00000000+0.j ] [ 0.00000000+0.j 0.70710678+0.70710678j]] """ u = qstate('u') d = qstate('d') Q = d * d.dag() + (exp(1.0j * theta) * u * u.dag()) return Qobj(Q)
def cnot(): """ Quantum object representing the CNOT gate. Returns ------- cnot_gate : qobj Quantum object representation of CNOT gate Examples -------- >>> cnot() Quantum object: dims = [[2, 2], [2, 2]], shape = [4, 4], type = oper, isHerm = True Qobj data = [[ 1.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 1.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 1.+0.j] [ 0.+0.j 0.+0.j 1.+0.j 0.+0.j]] """ uu = qstate('uu') ud = qstate('ud') du = qstate('du') dd = qstate('dd') Q = dd * dd.dag() + du * du.dag() + uu * ud.dag() + ud * uu.dag() return Qobj(Q)
def phasegate(theta): """ Returns quantum object representing the phase shift gate. Parameters ---------- theta : float Phase rotation angle. Returns ------- phase_gate : qobj Quantum object representation of phase shift gate. Examples -------- >>> phasegate(pi/4) Quantum object: dims = [[2], [2]], \ shape = [2, 2], type = oper, isHerm = False Qobj data = [[ 1.00000000+0.j 0.00000000+0.j ] [ 0.00000000+0.j 0.70710678+0.70710678j]] """ u = qstate("u") d = qstate("d") Q = d * d.dag() + (exp(1.0j * theta) * u * u.dag()) return Qobj(Q)
def cnot(): """ Quantum object representing the CNOT gate. Returns ------- cnot_gate : qobj Quantum object representation of CNOT gate Examples -------- >>> cnot() Quantum object: dims = [[2, 2], [2, 2]], \ shape = [4, 4], type = oper, isHerm = True Qobj data = [[ 1.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 1.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 1.+0.j] [ 0.+0.j 0.+0.j 1.+0.j 0.+0.j]] """ uu = qstate("uu") ud = qstate("ud") du = qstate("du") dd = qstate("dd") Q = dd * dd.dag() + du * du.dag() + uu * ud.dag() + ud * uu.dag() return Qobj(Q)
def swap(N=None, control=None, target=None, mask=None): """Quantum object representing the SWAP gate. Returns ------- swap_gate : qobj Quantum object representation of SWAP gate Examples -------- >>> swap() Quantum object: dims = [[2, 2], [2, 2]], \ shape = [4, 4], type = oper, isHerm = True Qobj data = [[ 1.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 1.+0.j 0.+0.j] [ 0.+0.j 1.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 1.+0.j]] """ if mask: warnings.warn("The mask argument to iswap is deprecated. " + "Use the N, control and target arguments instead.") if sum(mask) != 2: raise ValueError("mask must only have two ones, rest zeros") dims = [2] * len(mask) idx, = where(mask) N = prod(dims) data = sp.lil_matrix((N, N)) for s1 in state_number_enumerate(dims): i1 = state_number_index(dims, s1) if s1[idx[0]] == s1[idx[1]]: i2 = i1 else: s2 = array(s1).copy() s2[idx[0]], s2[idx[1]] = s2[idx[1]], s2[idx[0]] i2 = state_number_index(dims, s2) data[i1, i2] = 1 return Qobj(data, dims=[dims, dims], shape=[N, N]) elif not N is None and not control is None and not target is None: return gate_expand_2toN(swap(), N, control, target) else: uu = qstate('uu') ud = qstate('ud') du = qstate('du') dd = qstate('dd') Q = uu * uu.dag() + ud * du.dag() + du * ud.dag() + dd * dd.dag() return Q
def toffoli(): """Quantum object representing the Toffoli gate. Returns ------- toff_gate : qobj Quantum object representation of Toffoli gate. Examples -------- >>> toffoli() Quantum object: dims = [[2, 2, 2], [2, 2, 2]], shape = [8, 8], type = oper, isHerm = True Qobj data = [[ 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j]] """ uuu = qstate('uuu') uud = qstate('uud') udu = qstate('udu') udd = qstate('udd') duu = qstate('duu') dud = qstate('dud') ddu = qstate('ddu') ddd = qstate('ddd') Q = ddd*ddd.dag() + ddu*ddu.dag() + dud*dud.dag() + duu*duu.dag() + \ udd*udd.dag() + udu*udu.dag() + uuu*uud.dag() + uud*uuu.dag() return Qobj(Q)
def swap(mask=None): """Quantum object representing the SWAP gate. Returns ------- swap_gate : qobj Quantum object representation of SWAP gate Examples -------- >>> swap() Quantum object: dims = [[2, 2], [2, 2]], \ shape = [4, 4], type = oper, isHerm = True Qobj data = [[ 1.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 1.+0.j 0.+0.j] [ 0.+0.j 1.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 1.+0.j]] """ if mask is None: uu = qstate('uu') ud = qstate('ud') du = qstate('du') dd = qstate('dd') Q = uu * uu.dag() + ud * du.dag() + du * ud.dag() + dd * dd.dag() return Qobj(Q) else: if sum(mask) != 2: raise ValueError("mask must only have two ones, rest zeros") dims = [2] * len(mask) idx, = where(mask) N = prod(dims) data = sp.lil_matrix((N, N)) for s1 in state_number_enumerate(dims): i1 = state_number_index(dims, s1) if s1[idx[0]] == s1[idx[1]]: i2 = i1 else: s2 = array(s1).copy() s2[idx[0]], s2[idx[1]] = s2[idx[1]], s2[idx[0]] i2 = state_number_index(dims, s2) data[i1, i2] = 1 return Qobj(data, dims=[dims, dims], shape=[N, N])
def snot(): """Quantum object representing the SNOT (Hadamard) gate. Returns ------- snot_gate : qobj Quantum object representation of SNOT (Hadamard) gate. Examples -------- >>> snot() Quantum object: dims = [[2], [2]], shape = [2, 2], type = oper, isHerm = True Qobj data = [[ 0.70710678+0.j 0.70710678+0.j] [ 0.70710678+0.j -0.70710678+0.j]] """ u=qstate('u') d=qstate('d') Q=1.0/sqrt(2.0)*(d*d.dag()+u*d.dag()+d*u.dag()-u*u.dag()) return Qobj(Q)
def snot(): """Quantum object representing the SNOT (Hadamard) gate. Returns ------- snot_gate : qobj Quantum object representation of SNOT (Hadamard) gate. Examples -------- >>> snot() Quantum object: dims = [[2], [2]], shape = [2, 2], type = oper, isHerm = True Qobj data = [[ 0.70710678+0.j 0.70710678+0.j] [ 0.70710678+0.j -0.70710678+0.j]] """ u = qstate('u') d = qstate('d') Q = 1.0 / sqrt(2.0) * (d * d.dag() + u * d.dag() + d * u.dag() - u * u.dag()) return Qobj(Q)
def fredkin(): """Quantum object representing the Fredkin gate. Returns ------- fred_gate : qobj Quantum object representation of Fredkin gate. Examples -------- >>> fredkin() Quantum object: dims = [[2, 2, 2], [2, 2, 2]], \ shape = [8, 8], type = oper, isHerm = True Qobj data = [[ 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j]] """ uuu = qstate("uuu") uud = qstate("uud") udu = qstate("udu") udd = qstate("udd") duu = qstate("duu") dud = qstate("dud") ddu = qstate("ddu") ddd = qstate("ddd") Q = ( ddd * ddd.dag() + ddu * ddu.dag() + dud * dud.dag() + duu * duu.dag() + udd * udd.dag() + uud * udu.dag() + udu * uud.dag() + uuu * uuu.dag() ) return Qobj(Q)
def toffoli(N=None, control1=None, control2=None, target=None): """Quantum object representing the Toffoli gate. Returns ------- toff_gate : qobj Quantum object representation of Toffoli gate. Examples -------- >>> toffoli() Quantum object: dims = [[2, 2, 2], [2, 2, 2]], \ shape = [8, 8], type = oper, isHerm = True Qobj data = [[ 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j] [ 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j]] """ if (not N is None and not control1 is None and not control2 is None and not target is None): return gate_expand_3toN(toffoli(), N, control1, control2, target) else: uuu = qstate('uuu') uud = qstate('uud') udu = qstate('udu') udd = qstate('udd') duu = qstate('duu') dud = qstate('dud') ddu = qstate('ddu') ddd = qstate('ddd') Q = ddd * ddd.dag() + ddu * ddu.dag() + dud * dud.dag() + \ duu * duu.dag() + udd * udd.dag() + udu * udu.dag() + \ uuu * uud.dag() + uud * uuu.dag() return Qobj(Q)