import java.io.FileNotFoundException;

/**
 * This class writes English and Picture files
 * for the exact compilation, discovered by CopperSmith,
 * of the Discrete Fourier Transform.
 * We follow the conventions of  
 * quant-ph 0407215, "QC Paulinesia" by R. Tucci.
 *
 */


public class FouSEO_writer  extends SEO_writer{
	//private String 	_filePrefix;
	//private int 		_numBits;


/**
 * 
 */	
	public FouSEO_writer(
	        String 	filePrefix,
	        int numBits)
	{ 	
	 	
    	super(filePrefix + "_qfou", numBits);
		
    	write();

        closeFiles();

	}
/**
 * 
 */
    public FouSEO_writer(
		java.io.PrintWriter  englishOut,
		java.io.PrintWriter  pictureOut,
		Boolean doWrite,
		int numBits,
		int numBitsInP0Pad)
    {
    	super(englishOut, pictureOut, numBits, numBitsInP0Pad);
    	
    	if(doWrite){write();}
    }
    
/**
 * 
 */
	public void write(){
		//permutation R
		for(int j = _numBits - 1; j>=1; j--){
			for(int k = j-1; k>=0; k--){
				writeBitSwap(j,k);
			}
		}


		Controls controls = new Controls(_numBits, 1);
		int j, k;
		
		for(k=0; k<_numBits; k++){
			writeOneBitGate(k, new OneBitHadamard());
			controls.setControl(0, k, true);
			for(j=k+1; j<_numBits; j++){
				//note j>k so abs(j-k) = j-k
		        writeControlledGate(
					j, //targetBitPosition, 
					controls, 
					new OneBitP1PhaseFactor(
					Math.PI/(1<<(j-k))));
			}
		}
				
		
	}
/**
 * 
 */
    public static void main(String[] args) throws FileNotFoundException{
    	
		int numBits = 4;
		int numBitsInP0Pad = 5;
		String 	filePrefix = "test";
		
		//English File
		java.io.File    engFile = 
			new java.io.File(filePrefix + "_eng.txt");
		java.io.PrintWriter englishOut = 
			new java.io.PrintWriter(engFile);

		//Picture File
		java.io.File    picFile = 
			new java.io.File(filePrefix + "_pic.txt");
		java.io.PrintWriter pictureOut = 
			new java.io.PrintWriter(picFile);
    	
		FouSEO_writer  w = new FouSEO_writer(
    			englishOut,
    			pictureOut,
    			true, //doWrite
    			numBits,
    			numBitsInP0Pad);
    	w.closeFiles();
    }

}
