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]

























The first version of the random generator uses the classical way of solving problems. This second version of the random generator uses the quantum way of solving problems. But unfortunately it does not work when you run it. So the professor has just suggested that I write a third version of this.



import java.io.*;

import static java.lang.Math.*;

import java.util.*;

/**
 * I. Here is what I did:
1.	I generated 3 numbers say:  4,6,2
2.	I generated a string say: 010.  0 match with 4, 1 match with 6, 0 match with2.
3.	I generated two decimals for real and imaginary coefficient.  (later I will normalize)
4.	I generate a 4 letter string say: 0001 this basically correspond to (7, 5, 3, 1)
5.	Next I insert 4,6,2 values into this string (7,6,5,4,3,2,1)=(0100001). This state has the coefficient I generated for (4,6,2).
6.	In this process I used two sorts.  The first is I sort 4,6,2 to 2,4,6 with corresponding bits.  This second sort is I sort the (7,6,5,4,3,2,1) states with rising index from (0000000) to (1111111).

II. What I mean by sum is:  First I took the squares of the real and imaginary coefficients and summed them to get the probabilities.  Then I sum the probabilities on the 127 null vectors.  

III. Here is how I verified the solutions
1.	Take the above example: 4,6,2 and if my solution string, say 1111111, has a 1 on 4, a 0 on 6, or a 1 on 2, I will increment counter by 1.  If counter>0 I will increase netcounter by 1, meaning I have satisfied this clause.  In this case I do indeed increment netcounter by 1.  
2.	Next, netcounter will have to equal the number of clauses that I generated say 15, to signify that one problem has been solved.
3.	Next I have 50 such problems that all have to be solved.
 * @param args
 */

import java.io.*;

import static java.lang.Math.*;

import java.util.*;




public class QSATsolver {

	private int clausesize = 3;
	private int numvariable = 7;
	private int numclause = 35;
	private int numinstances = 5;
	private ArrayList<String> stack = new ArrayList<String>(); //nonnullspace list
	private ArrayList<String> chain = new ArrayList<String>();
	private int success= 0;
	private ArrayList<Integer> temp2var = new ArrayList <Integer> ();
	private ArrayList<ArrayList <Integer>> temp5var = new ArrayList <ArrayList<Integer>> ();
	private ArrayList <State> completeState = new ArrayList <State>();
	private ArrayList<ArrayList <State>> completeStateStack = new ArrayList<ArrayList <State>>();
	private ArrayList<Integer> left = new ArrayList<Integer>();
	private ArrayList<Integer> right= new ArrayList<Integer>();
	private ArrayList <State> sumStack = new ArrayList<State>();
	private ArrayList <State> solutionStack = new ArrayList<State>();
	private ArrayList<State> oneNullState= new ArrayList<State>();
	private ArrayList <ArrayList<State>> nullVectorStack= new ArrayList<ArrayList<State>>();
	private ArrayList <ArrayList<State>> nullVectorStack2= new ArrayList<ArrayList<State>>();
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		QSATsolver qs = new QSATsolver();
		qs.generate();
	}
	public void generate(){
		
		ArrayList<Double>temp = new ArrayList<Double>();
		ArrayList<ArrayList<Double>> temp2 = new ArrayList<ArrayList<Double>>();
		ArrayList<ArrayList<ArrayList<Double>>> temp5 = new ArrayList<ArrayList<ArrayList<Double>>>();
		double length = 0;
		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 max = (int)pow(2,clausesize);
				for (int i = 0; i<max; i++){
					double real =  Math.random();
					temp.add(new Double(real));
					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;
					}
				}
				if(length==0){
					length=1;
				}
				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>>(); //8 items in these
				temp5var.add(temp2var);
				temp2var = new ArrayList <Integer> ();
				length=0;
			}
			//inside each instance renew temp5var and temp5
			
			generateState(temp5);
			
			System.out.println(completeStateStack.size());
	
			int max = (int)pow(2,numvariable);
			double realsum=0;
			String label="";
			for (int t = 0; t < max; t++){
				for (int z = 0; z < completeStateStack.size(); z++){
					ArrayList <State> cs = completeStateStack.get(z);
					realsum += cs.get(t).probab;
					label = cs.get(t).label;
				}
				State tempstate = new State(label,realsum,Todecimal(label));
				realsum = 0;
				sumStack.add(tempstate);			
			}
			
			generateNullVector();
			orthogonalizeNullVector();	
			normalizeNullVector();
			
			
			double realsum2=0;		
			for (int t = 0; t<max; t++){
				for (int z = 0; z<nullVectorStack2.size(); z++){
					ArrayList <State> cs = nullVectorStack2.get(z);
					realsum2 += cs.get(t).probab;
					label = cs.get(t).label;
				}
				State tempstate = new State(label,realsum2,Todecimal(label));
				solutionStack.add(tempstate);			
			}
				
			ArrayList<String> solution = findSolution();
			
			double current=0;
			for(int h = 0; h<temp5.size(); h++){
				ArrayList<ArrayList<Double>> atemp2st= temp5.get(h);
				int temp1=0;
				for(int j= 0; j<atemp2st.size(); j++){
					ArrayList<Double> list = atemp2st.get(j);
					double realtemp = Double.parseDouble(""+list.get(0));
					double probab = realtemp;
					if(probab>current){
						current=probab;
						temp1 = j;
					}
				}
				String temp3 = Tobinary(temp1,"");
				temp3 = reverseString(temp3);
				if (temp3.length()<clausesize) {
					temp3 = addZero(temp3,clausesize);
				}
				stack.add(temp3);
			}
			
			if(varifySolution(solution)){
				success++;
			}
			
			temp5var = new ArrayList <ArrayList<Integer>> ();
			temp5 = new ArrayList<ArrayList<ArrayList<Double>>>();
			completeStateStack = new ArrayList<ArrayList <State>>();
			nullVectorStack2= new ArrayList<ArrayList<State>>();
			nullVectorStack= new ArrayList<ArrayList<State>>();
			sumStack = new ArrayList<State>();
			solutionStack = new ArrayList<State>();
			stack = new ArrayList<String>();
		}
		System.out.println(success);
	}
	public boolean varifySolution(ArrayList<String> solution){
		boolean isSolution =false;
		int netCount=0;
		int counter=0;
		String sol="";
		
		for(int w=0; w<solution.size(); w++){
			sol=solution.get(w);
			for(int i=0; i<temp5var.size(); i++){
				ArrayList<Integer> aclause = temp5var.get(i);
				String bclause = stack.get(i);
				int [] varnumber = new int[clausesize];
				for(int j = 0; j<aclause.size(); j++) {
					varnumber[j] = Integer.parseInt(""+aclause.get(j))-1;
				}
				
				String [] pattern = new String [clausesize];
				int [] pattern2 = new int[clausesize];
				String [] pattern3 = new String [numvariable];
				int [] pattern4 = new int[numvariable];
				for (int s=0; s<clausesize; s++) {
					pattern[s] = ""+bclause.charAt(clausesize-1-s);
					pattern2[s] = Integer.parseInt(pattern[s]);
				}
				int [] varnumber2 = getCopy(varnumber);
				sort(varnumber2, pattern2);
				
				for (int s=0; s<numvariable; s++) {
					pattern3[s] = ""+sol.charAt(numvariable-1-s);
					pattern4[s] = Integer.parseInt(pattern3[s]);
				}
				for (int u =0; u<clausesize; u++){
					if(pattern2[u]==0){
						if(pattern4[varnumber2[u]]==1){
							counter++;
						}
					} else {
						if(pattern4[varnumber2[u]]==0){
							counter++;
						}
					}
				}
				if(counter>0){
					netCount++;
				}
				counter=0;
			}
			if(netCount==numclause){
				isSolution=true;
				break;
			}
		}
		return isSolution;
	}
	public ArrayList<String> findSolution(){
		double current=0;
		ArrayList<String> sols= new ArrayList<String>();
		
		for (int i=0; i<solutionStack.size(); i++){
			State tempstate = solutionStack.get(i);
			double probability = Math.abs(tempstate.probab);
			if(probability>current){
				current=probability;
			}
		}
		for (int i=0; i<solutionStack.size(); i++){
			State tempstate = solutionStack.get(i);
			double probability = Math.abs(tempstate.probab);
			if(probability==current){
				sols.add(tempstate.label);
			}
		}
		return sols;
	}
	public void generateNullVector(){
		double tempreal = sumStack.get(0).probab;
		State bstate;
		for(int i=1; i<sumStack.size(); i++){
			double tempreal2 = sumStack.get(i).probab;
			for(int j = 0; j < sumStack.size(); j++){
				State astate = sumStack.get(j);
				if(j==0){
					bstate = new State(astate.label,tempreal2,Todecimal(astate.label));
				}
				else if(j==i){
					bstate = new State(astate.label,-1*tempreal,Todecimal(astate.label));
				}
				else {
					bstate = new State(astate.label,0,Todecimal(astate.label));
				}
				oneNullState.add(bstate);
			}
			nullVectorStack.add(oneNullState);
			oneNullState= new ArrayList<State>();
		}
	}
	public double dotProduct (double r1, double r2){
		double dprod;
		dprod = r1*r2;
		return dprod;
	}
	public double dotProduct2 (ArrayList<State>st1, ArrayList<State>st2){
		double dproduct;
		double dproduct2;
		double sumdproduct= 0;
		double sumdproduct2=0;
		for(int i=0; i<st1.size(); i++){
			double b1 = st1.get(i).probab;
			double c1 = st2.get(i).probab;
			dproduct = dotProduct(b1, c1);
			dproduct2 = dotProduct(c1,c1);
			sumdproduct+=dproduct;
			sumdproduct2+=dproduct2;
		}
		return sumdproduct/sumdproduct2;
	}
	public void orthogonalizeNullVector(){
		nullVectorStack2.add(nullVectorStack.get(0));		
		for(int i = 1; i<nullVectorStack.size(); i++){
			ArrayList<State>oneState= nullVectorStack.get(i);
			ArrayList<State>oneState2 = clone2(oneState);
			for(int j = 0; j<i; j++){
				ArrayList<State>twoState= nullVectorStack2.get(j);
				double coef = dotProduct2(oneState, twoState);
				ArrayList<State>twoState2 = clone2(twoState);
				eliminate(oneState2, multiply(coef, twoState2));
			}
			nullVectorStack2.add(oneState2);
		}
		//notice that nullVectorStack was never altered 
		//nullVectorStack2 is completely independent of nullVectorStack2
	}
	public ArrayList<State> clone2(ArrayList<State> astate){
		ArrayList<State> temp1= new ArrayList<State>();
		for(int i=0; i<astate.size(); i++){
			State st = astate.get(i);
			State st2 = new State(st.label, st.probab, st.index);
			temp1.add(st2);
		}
		return temp1;
	}
	public void eliminate (ArrayList<State> st2, ArrayList<State> st1){
		for (int i = 0; i<st2.size(); i++){
			State one = st2.get(i);
			State two = st1.get(i);
			st2.get(i).probab = one.probab-two.probab;			
		}
	}
	public ArrayList <State> multiply (double c, ArrayList<State> o){
		ArrayList <State> astate = new ArrayList<State>();
		for(int i = 0; i<o.size(); i++){
			State st = o.get(i);
			double one = st.probab;
			double one2 = c*one;
			st.probab = one2;
			astate.add(st);
		}
		return astate;
	}
	public void normalizeNullVector(){
		double length=0;
		for(int i = 0; i<nullVectorStack2.size(); i++){
			ArrayList<State> ststack = nullVectorStack2.get(i);
			for(int j = 0; j<ststack.size(); j++){
				State st = ststack.get(j);
				length+=st.probab;
			}
			if(length==0){
				length=1;
			}
			for(int j = 0; j<ststack.size(); j++){
				State st = ststack.get(j);
				st.probab = st.probab/length;
			}
		}		
	}
	public void generateState(ArrayList<ArrayList<ArrayList<Double>>> temp5){
		int [] varnumber = new int [clausesize];
		
		String myleft, myright;
		ArrayList <State> temp2again = new ArrayList <State>();
		ArrayList<ArrayList <State>> temp5again = new ArrayList <ArrayList <State>>();
		
		int max = (int)pow(2,numvariable-clausesize);
		for(int j = 0; j<max; j++) {
			String temp3 = Tobinary(j,"");
			temp3 = reverseString(temp3);
			if (temp3.length()<(numvariable-clausesize)) {
				temp3 = addZero(temp3, (numvariable-clausesize));
			}
			if((numvariable-clausesize)>0) {
				chain.add(temp3);
			}
		}
		int max2 = (int) pow(2,clausesize);
		
		for (int g = 0; g < temp5.size(); g++) { //temp5 has 15 elements
			ArrayList<ArrayList<Double>> clause = temp5.get(g); //the 8 element clause
			for(int m = 0; m < clause.size(); m++) {
				ArrayList<Double> list = clause.get(m); //the 2 element complex number	
				String temp3 = Tobinary(m,"");
				temp3 = reverseString(temp3);
				if (temp3.length() < clausesize) {
					temp3 = addZero(temp3, clausesize);
				}
				double x1 = Double.parseDouble(""+list.get(0));
				temp2again.add(new State(temp3, x1));				
			}
			temp5again.add(temp2again);
			temp2again = new ArrayList <State>();
		}
		for (int g = 0; g<temp5var.size(); g++){
			//temp5var has 15 elements.
			ArrayList<Integer> list1 = temp5var.get(g); //list1 is a temp2var:3 int list
			for(int i = 0; i<list1.size(); i++) {
				varnumber[i] = Integer.parseInt(""+list1.get(i))-1;
				System.out.print(varnumber[i]+" "); //prints
				//varnumber[i] value start at 0 not 1.
			}
			System.out.println("");//prints
			String [] pattern = new String [clausesize];
			int [] pattern2 = new int[clausesize];
			ArrayList <State> list2 = temp5again.get(g);  //list2 has 8 elements
			for(int h=0; h<list2.size(); h++){
				State st = list2.get(h); //st is a State
				double one = st.probab/max;
				String pat = st.label;
				for (int s = 0; s < clausesize; s++) {
					pattern[s] = ""+pat.charAt(clausesize-1-s);
					pattern2[s] = Integer.parseInt(pattern[s]);
				}
				int [] varnumber2 = getCopy(varnumber);
				sort(varnumber2, pattern2);
				
				for(int l = 0; l < chain.size(); l++){
					String str = chain.get(l);
					for (int k = 0; k < varnumber2.length; k++){
						String insertion = ""+pattern2[k];
						if(varnumber2[k] == 0){
							myleft=str;
							myright="";
						}
						else if(varnumber2[k] == numvariable-1){
							myright=str;
							myleft="";
						}
						else {
							myright = str.substring(str.length()-varnumber2[k]);
							myleft = str.substring(0, str.length()-varnumber2[k]);
						}
						str = ""+myleft+insertion+myright;
					}
					State ast = new State(str, one, Todecimal(str));
					completeState.add(ast);
				}
				if(chain.size()==0) {
					State ast = new State(pat, one, Todecimal(pat));
					completeState.add(ast);
				}
				
			} //completeState has 2^7 component states
			
			//now we are going to put the 128 pure states in order
			String [] a1 = new String [completeState.size()];
			double [] a2 = new double [completeState.size()];
			int [] a4 = new int [completeState.size()];
			int totalstates = (int) pow (2,numvariable);
			
			for(int w = 0; w<completeState.size(); w++){
				State onest = completeState.get(w);
				a1[w] = onest.label;
				a2[w] = onest.probab;
				a4[w] = onest.index;
			}
			sort2(a1, a2, a4); //starting with 0000000 end with 1111111
			completeState =  new ArrayList <State>();
			for (int c = 0; c < totalstates; c++) {
				State tempstate = new State(a1[c], a2[c], a4[c]);
				completeState.add(tempstate);
			}
			double length=0;
			for(int u=0; u<completeState.size(); u++){
				State st4= completeState.get(u);
				length+=st4.probab;
			}
			if(length==0){
				length=1;
			}
			for(int u=0; u<completeState.size(); u++){
				State st4= completeState.get(u);
				st4.probab/=length;
			}
			completeStateStack.add(completeState);
			completeState =  new ArrayList <State>();			
			//just inside temp5var iter.
		}
	}
	public int [] getCopy(int [] varnumber){
		int[] oldvarnumber = new int [varnumber.length];
		for(int i = 0; i<varnumber.length; i++) {
			oldvarnumber[i] = varnumber[i];
		}
		return oldvarnumber;
	}
	public void sort(int [] a, int [] b){
		int temp = 0;
		int temp2;
		int temp3;
		for(int i = 0; i<a.length-1; i++){
			temp = i;
			for(int j = i+1; j<a.length; j++){
				if(a[j]<a[temp]){
					temp = j;
				}
			}
			temp2 = a[temp];
			a[temp] = a[i];
			a[i] = temp2;
			
			temp3 = b[temp];
			b[temp] = b[i];
			b[i] = temp3;
		}
	}
	public void sort2(String[] str, double [] a, int []strval ){
		int temp = 0;
		int temp2;
		double temp3;
		double temp4;
		String temp5;
		for(int i = 0; i<strval.length-1; i++){
			temp = i;
			for(int j = i+1; j<strval.length; j++){
				if(strval[j]<strval[temp]){
					temp = j;
				}
			}
			temp2 = strval[temp];
			strval[temp] = strval[i];
			strval[i] = temp2;
		
			temp4 = a[temp];
			a[temp] = a[i];
			a[i] = temp4;
			
			temp5 = str[temp];
			str[temp] = str[i];
			str[i] = temp5;
		}
	}
	public String addZero(String s, int l){
		int b = l-s.length();
		for(int i = 0; i<b; i++){
			s = "0"+""+s;
		}
		return s;
	}
	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 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;
	}
	private class State {
		String label;
		double probab;
		int index;

		public State(String str, double a){
			label = str;
			probab = a;
		}
		public State( double a){
			probab = a;
		}
		public State(String str, double a, int ind){
			label = str;
			probab = a;
			index = ind;
		}
	}
}