Chris Pollett > Students > Yunxuan

    Print View

    [Bio]

    [Blog]

    [First Proposal]

    [Final Step of Shor's and Grover's Algorithms]

    [jQuantum QFT]

    [My Quantum Circuits]

    [Three Models]

    [Threshold of Error Correction]

    [jQuantum Manual]

    [Quantum State and LH]

    [QHC Scientists]

    [LH and Tensor Networks]

    [k-LH is QMA-complete]

    [Deutch Josza Algorithm]

    [Semester Report]

    [Second Proposal]

    [SolveQ Algorithm: 2-SAT]

    [Random-kSAT-Generator: Version 1]

    [Freezing Point Experiment]

    [Random-kQSAT-Generator: Version 2]

    [Random-kQSAT-Generator: Version 3]

    [Antiferromagnetic Heisenberg Model]

    [Ising Model]

    [Random-kQSAT-Generator: Version 4]

    [Random-kQSAT-Generator: Version 5]

    [Random-kQSAT-Generator: Version 6]

    [SolveQ Algorithm: 2-QSAT]

    [Thesis]


























This program works fine. There are suppose to be no true null vectors. But below a threshold of 0.0055 count as a null vector. This is the version with: permutator, no repeating variable, and threshold. The freezing point ratio is 0.571.

import java.io.*;

import static java.lang.Math.*;

import java.util.*;


public class QSATsolver3 {
	private int clausesize = 3;
	private int numvariable = 7; //crashes for 10 variable
	private int numclause = 4;
	private int numinstances =50;
	private ArrayList<Integer> temp2var = new ArrayList <Integer> ();
	private ArrayList<ArrayList <Integer>> temp5var = new ArrayList <ArrayList<Integer>> ();
	private ArrayList<String> chain = new ArrayList<String>();
	private ArrayList<String> stack = new ArrayList<String>();
	private ArrayList<double[][]> piMatrixStack = new ArrayList<double[][]>();
	private ArrayList<double[][]> realStack = new ArrayList<double[][]>();
	private ArrayList<ArrayList<Double>> temp2 = new ArrayList<ArrayList<Double>>();
	private ArrayList<ArrayList<ArrayList<Double>>> temp5 = new ArrayList<ArrayList<ArrayList<Double>>>();
	private ArrayList<Double>temp = new ArrayList<Double>();
	private int netCount = 0;
	private int counter3=0;
	private double threshold=0.0055;
	
	private static final double Epsilon = 1e-10;
	
	public static void main(String[] args) {
		QSATsolver3 qs = new QSATsolver3();
		qs.generate();
	}
	public void generate(){
		double length = 0;
		int max = (int)pow(2,numvariable);
		boolean []taken = new boolean [numvariable];
		
		for(int i=0; i<taken.length; i++){
			taken[i] = false;
		}
		for (int k = 0; k < numinstances; k++) {
			for (int j = 0; j < numclause; j++) {
				int v;
				for(int i = 0; i < clausesize; i++){
					v = (int)(Math.random()*numvariable)+1;
					while(taken[v-1] == true){
						v = (int)(Math.random()*numvariable)+1;					
					}
					taken[v-1] = true;
					temp2var.add(new Integer(v));
				
				}	
				
				for(int i = 0; i < taken.length; i++){
					taken[i] = false;
				}
				int max2 = (int)pow(2,clausesize);
				for (int i = 0; i < max2; i++){
					double probab = Math.random();
					temp.add(new Double(probab));
					temp2.add(temp);
					temp = new ArrayList<Double>();
				}
				for (int i = 0; i < temp2.size(); i++){
					ArrayList <Double> list = temp2.get(i);
					for (int g = 0; g < list.size(); g++){
						double a = Double.parseDouble(""+list.get(g));
						length+=a;
					}
				}
				
				//length = sqrt(length);
				for (int i = 0; i < temp2.size(); i++){
					ArrayList <Double> list = temp2.get(i);
					for (int g = 0; g < list.size(); g++){
						double c = Double.parseDouble(""+list.get(g));
						c = c/length;
						list.remove(g);
						list.add(g, new Double(c));
					}
				}
				
				temp5.add(temp2); //15 items in these
				temp2 = new ArrayList<ArrayList<Double>>(); 
				temp5var.add(temp2var);
				temp2var =  new ArrayList <Integer> ();
				//chain = new ArrayList<String>();
				length=0;
			}
			makeAllClauses();
			if(calculateMatrix()){
				netCount++;
			}
			piMatrixStack = new ArrayList<double[][]>();
			realStack = new ArrayList<double[][]>();
			temp5var = new ArrayList <ArrayList<Integer>> ();
			temp5 = new ArrayList<ArrayList<ArrayList<Double>>>();
			
			System.out.println(counter3);
			counter3=0;
		}
		System.out.println("Count:"+ netCount);
	}
	
	public boolean calculateMatrix(){
		double[][] temp1, temp2;		
		int max = (int)pow(2,numvariable);
		double [][]sumtemp1 = new double [max][max];
		temp1= new double[max][max];
		boolean success = false;
		
		for(int i=0; i<piMatrixStack.size(); i++){
			double[][] one = piMatrixStack.get(i);
			double[][] two = realStack.get(i);
			double[][] invone = makeTranspose(one);
			temp1 = matMultiply(matMultiply(one,two),invone);
			for(int j = 0; j < temp1.length; j++){
				for(int k = 0; k < temp1.length; k++){
					sumtemp1[j][k] += temp1[j][k];
				}
			}
		}
		/*for(int i=0; i<sumtemp1.length; i++){
			for(int j=0; j<sumtemp1.length; j++){
				System.out.print(sumtemp1[i][j]+" ");
			}
			System.out.println();
		}*/
		//System.out.println();
		boolean [] x= gaussianElimination(sumtemp1,1);
		success = nullvecExist(x);
		return success;
	}
	public boolean nullvecExist(boolean [] x){
		for(int i=0; i<x.length; i++){
			if(x[i]==true){
				//System.out.print(i+" ");
				counter3++;
			}
		}
		for(int i=0; i<x.length; i++){
			if(x[i]==true){
				return true;
			}
		}
		return false;
	}
	public void swaprows(double[][]matrix){
		//int zerocount = 0;
		int max = matrix.length;
		double [] temp;
		int current = 0;
		double ratio = 1;
		boolean found = false;
		
		//current is the line number you have just fixed.  
		//i is all the line numbers that you are working on still.
		//the point of this part is to order rows by increasing zero count.
		/*for(int i=0; i<max; i++){
			System.out.print(zeroCount2(matrix[i])+" ");
		}*/
		current=0;
		int currentCount=0;
		while(current<max){
			for(int i = current; i < max; i++){
				if(zeroCount2(matrix[i]) == currentCount){
					temp = matrix[i];
					matrix[i] = matrix[current];
					matrix[current] = temp;		
					current++;
				}
			}
			currentCount++;
		}
	}
	public boolean [] gaussianElimination(double [][]matrix, int type){
		int max = matrix.length;
		double ratio = 1;
		boolean [] sol= new boolean [max];
		
		swaprows(matrix);
		int ind=0;
		int oldind=ind;
		int count = zeroCount2(matrix[ind]);
		while(ind+1<max){
			for(int i = ind+1; i<max; i++){
				int c = zeroCount2(matrix[i]);
				if(c==count) {
					ratio = matrix[i][count]/matrix[ind][count];
					for(int w = 0; w < matrix.length; w++){
						matrix[i][w]-=matrix[ind][w]*ratio;
					}
				} else if(c>count){
					break;
				}
			}
			swaprows(matrix);
			for(int i=1; i<max; i++){
				if(zeroCount2(matrix[i])==zeroCount2(matrix[i-1])){
					ind=i-1;
					break;
				}
				if(i==max-1){
					ind=max-1;
				}
			}
			
			count=zeroCount2(matrix[ind]);
			if(count==max){
				break;
			}
		}
	
		for(int i=0; i<sol.length; i++){
			sol[i]=true;
		}
		
		for(int i=0; i<matrix.length; i++){
			if(Math.abs(matrix[i][i])>threshold){
				sol[i]=false;
			}
		}
		return sol;
	}
	public int zeroCount2 (double[] vec){
		int temp = 0;
		for(int i = 0; i < vec.length; i++){
			if(vec[i]==0){
				temp++;
			} else{
				break;
			}
		}
		return temp;
	}
	public double[][] makeTranspose(double[][] mat){
		double [][]temp = new double[mat.length][mat.length];
		for(int i = 0; i < mat.length; i++){
			for(int j = 0; j<mat[0].length; j++){
				temp[i][j] = mat[j][i];
			}
		}
		return temp;
	}
	public void makePi(){
		int max = (int)pow(2,numvariable);
		
		for(int j = 0; j<max; j++) {
			String temp3 = Tobinary(j,"");
			temp3 = reverseString(temp3);
			if (temp3.length()<numvariable) {
				temp3 = addZero(temp3, numvariable);
			}
			chain.add(temp3);
		}
	}
	public void makeAllClauses(){
		int max = (int)pow(2,numvariable);
		int[] temp1 = new int[clausesize];
		int[] temp3 = new int[numvariable-clausesize];
		boolean[] taken = new boolean[numvariable];
		int[] path = new int[numvariable];
		
		for(int i = 0; i < taken.length; i++){
			taken[i] = false;
		}
		int max2 = (int)pow(2,clausesize);
		for(int i = 0; i<temp5var.size(); i++){
			ArrayList<Integer> alist = temp5var.get(i);
			ArrayList<ArrayList<Double>> blist = temp5.get(i);
			for(int j = 0; j < alist.size(); j++){
				int temp2 = (int)(Integer.parseInt(""+alist.get(j)))-1;
				temp1[j] = temp2;
				taken[temp2] = true;
			}
			int ind = 0;
			for(int j = 0; j < (numvariable); j++){
				if(!taken[j]){
					temp3[ind] = j;
					ind++;
				}
			}
			for(int j = 0; j < taken.length; j++){
				taken[j] = false;
			}
			for(int j = 0; j < numvariable; j++){
				if(j<clausesize){
					path[j] = temp1[j];
				}else {
					path[j] = temp3[j-clausesize];
				}
			}
			double[][] onePi = makeProjector(path, i);
			piMatrixStack.add(onePi);
			double [][]oneClause=new double[max][max];
			
			for(int u=0; u<max2; u++){

				ArrayList<Double> list3= blist.get(u);
				double factor= Double.parseDouble(""+list3.get(0));
				String str2 = Tobinary(u,"");
				str2 = reverseString(str2);
				if (str2.length()<clausesize) {
					str2 = addZero(str2, clausesize);
				}
				String str = getI()+""+reverseString(str2);
				double[][]one = makeSig(str);
				for(int l=0; l<one.length; l++){
					for(int m=0; m<one.length; m++){
						oneClause[l][m]+=factor*one[l][m];
					}
				}
			}
			double length=0;
			for(int p=0; p<oneClause.length; p++){
				length+=oneClause[p][p];
			}
			for(int p=0; p<oneClause.length; p++){
				oneClause[p][p]=oneClause[p][p]/length;
			}
			realStack.add(oneClause);
		}
	}
	public String getI(){
		String temp = "";
		for(int i = 0; i < numvariable-clausesize; i++){
			temp = temp+"I";
		}
		return temp;
	}
	public double[][] makeSig (String path){

		char next;
		double a0 = 0;
		double a1 = 0;
		double a2 = 0;
		double a3 = 0;
		double[][]ul,ur,ll,lr;
		double [][] orig = new double[2][2];
		if(path.charAt(path.length()-1)=='I'){
			orig[0][0] = 1;
			orig[0][1] = 0;
			orig[1][0] = 0;
			orig[1][1] = 1;
		}else {
			if(path.charAt(path.length()-1)=='0'){
				orig[0][0] = 1;
				orig[0][1] = 0;
				orig[1][0] = 0;
				orig[1][1] = 0;
			}else if(path.charAt(path.length()-1)=='1'){
				orig[0][0] = 0;
				orig[0][1] = 0;
				orig[1][0] = 0;
				orig[1][1] = 1;
			}
		}
		for(int i = path.length()-2; i>=0; i--){
			next = path.charAt(i);
			if(next=='I'){
				a0 = 1;
				a1 = 0;
				a2 = 0;
				a3 = 1;
			}else {
				if(next=='0'){
					a0 = 1;
					a1 = 0;
					a2 = 0;
					a3 = 0;
				}else if(next=='1'){
					a0 = 0;
					a1 = 0;
					a2 = 0;
					a3 = 1;
				}
			}
			ul = multiply(a0, clone(orig));
			ur = multiply(a1, clone(orig));
			ll = multiply(a2, clone(orig));
			lr = multiply(a3, clone(orig));
			orig = makeLarge(ul, ur, ll, lr);
		}
		
		return orig;
	}
	public double[][] multiply(double c, double[][] mat){
		double [][] temp = new double[mat.length][mat.length];
		for (int i = 0; i<mat.length; i++){
			for (int j = 0; j<mat[0].length; j++){
				temp[i][j] = c*mat[i][j];
			}
		}
		return temp;
	}
	public double[][] makeLarge(double[][] ul, double[][] ur, double[][] ll, double[][]lr){
		double [][] temp = new double[ul.length*2][ul.length*2];
		for(int i = 0; i<ul.length; i++){
			for(int j = 0; j<ul.length; j++){
				temp[i][j] = ul[i][j];
			}
		}
		for(int i = 0; i<ul.length; i++){
			for(int j = 0; j<ul.length; j++){
				temp[i][j+ul.length] = ur[i][j];
			}
		}
		for(int i = 0; i<ul.length; i++){
			for(int j = 0; j<ul.length; j++){
				temp[i+ul.length][j] = ll[i][j];
			}
		}
		for(int i = 0; i<ul.length; i++){
			for(int j = 0; j<ul.length; j++){
				temp[i+ul.length][j+ul.length] = lr[i][j];
			}
		}
		return temp;
	}
	public double[][] clone(double[][] mat){
		double [][]temp = new double[mat.length][mat.length];
		for (int i = 0; i<mat.length; i++){
			for(int j = 0; j<mat[0].length; j++){
				temp[i][j] = mat[i][j];
			}
		}
		return temp;
	}
	public double[][] makeProjector(int [] path, int ind) {
		String str = "";
		int zeroCount = 0;
		int max = (int)pow(2,numvariable);
		double [] row = new double[max];
		double[][] matrix= new double[max][max];
		int [] bits= new int [numvariable];
		chain = new ArrayList<String>();
		makePi();
		for(int i=0; i<chain.size(); i++){
			String temp1 = chain.get(i);
			for(int j = 0; j<path.length; j++){
				bits[path[numvariable-1-j]]=Integer.parseInt(""+temp1.charAt(j));
			}
			for(int j=bits.length-1; j>=0; j--){
				str=str+""+bits[j];
			}
			zeroCount = Todecimal(str);
			for(int j = 0; j<max; j++){
				if(j==zeroCount){
					row[j] = 1;
				}else {
					row[j] = 0;
				}
			}
			for(int j = 0; j<max; j++){
				matrix[i][j] = row[j];
			}
			
			str = "";
		}
		/*for(int i=0; i<chain.size(); i++){
			String temp1 = chain.get(i);
			for(int j = path.length-1; j>=0; j--){
				str = str+temp1.charAt(numvariable-path[j]-1);
			}
			zeroCount = Todecimal(str);
			for(int j = 0; j<max; j++){
				if(j==zeroCount){
					row[j] = 1;
				}else {
					row[j] = 0;
				}
			}
			for(int j = 0; j<max; j++){
				matrix[i][j] = row[j];
				//if(i==1)
					//System.out.println(i);
			}
			str = "";
		}*/
		return matrix;
	}
	public double[][] matMultiply(double[][]mat1, double[][] mat2){
		double[][]prod = new double[mat1.length][mat1.length];
		for(int i = 0; i<prod.length; i++){
			for(int j = 0; j<prod.length; j++) {
				prod[i][j] = 0;
			}
		}
		for(int i = 0; i<prod.length; i++){
			for(int j = 0; j<prod.length; j++) {
				for(int k = 0; k<prod.length; k++){
					prod[i][j] += mat1[i][k]*mat2[k][j];
				}
			}
		}
		return prod;
	}
	public int Todecimal (String binary){
		int sum=0;
		for (int i=binary.length()-1; i>=0; i--){
			String bit = ""+binary.charAt(i);
			int bitval = Integer.parseInt(bit);
			int factor = (int)pow(2, binary.length()-1-i);
			sum += bitval*factor;
		}
		return sum;
	}
	public String reverseString(String st){
		String temp = "";
		for(int i = st.length()-1; i>=0; i--){
			temp = temp+st.charAt(i);
		}
		return temp;
	}
	
	
	public String Tobinary(int decimal, String path){
		int remainder;
		if(decimal<=1){
			path = path+decimal;
			return path;
		}
		remainder = decimal % 2;
		path = path+remainder;
		return Tobinary(decimal>>1, path);
	}
	
	
	public String addZero(String s, int l){
		int b = l-s.length();
		for(int i = 0; i<b; i++){
			s = "0"+""+s;
		}
		return s;
	}
}