R/Uf.R

Defines functions Uf

Documented in Uf

#' @export
Uf <- function(fun,n,m){
	Um <- matrix(rep(0,(2^(n+m))^2),nrow=2^(m+n))	#2^(n+m) x 2^(n+m)
	f <- match.fun(fun)
	#Iterate over each 2^n inputs
	for(x in 0:(2^n-1)){
		#compute f(x), which will determine the matrix along the diagonal
		b <- as.character(rev(intToBits(f(x))))					#get bit string of function output
		l <- length(b)
		if(b[l-m+1] == "00"){
			g <- I()
		} else{
			g <- X()
		}
		if(m >= 2){
			for(j in (l-m+2):l){
				if(b[j] == "00"){
					g <- tensor(g,I())
				} else{
					g <- tensor(g,X())
				}
			}
		}
		Um[ ((2^m)*x+1):((2^m)*(x+1)) , ((2^m)*x+1):((2^m)*(x+1)) ] <- g
		#print(((2^m)*x+1):((2^m)*(x+1)))
		#print(((2^m)*x+1):((2^m)*(x+1)))
		#print(g)
	}
	Um
}

Try the QuantumOps package in your browser

Any scripts or data that you put into this service are public.

QuantumOps documentation built on Feb. 3, 2020, 5:07 p.m.