Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://github.com/epiqc/ScaffCC
19 June 2025, 09:11:09 UTC
  • Code
  • Branches (10)
  • Releases (1)
  • Visits
    • Branches
    • Releases
    • HEAD
    • refs/heads/ScaffCC_OSX
    • refs/heads/master
    • refs/tags/2.2
    • refs/tags/5.0
    • refs/tags/v1.0
    • refs/tags/v1.0-beta.2
    • refs/tags/v2.0
    • refs/tags/v2.1
    • refs/tags/v3.0
    • refs/tags/v4.0
    • v3.1
  • 6bbaf7c
  • /
  • test_cases
  • /
  • Square_Root
  • /
  • square_root.n10.scaffold
Raw File Download
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
origin badgecontent badge Iframe embedding
swh:1:cnt:8f32bd2309389257f1684a336738c6a101f1a6f3
origin badgedirectory badge Iframe embedding
swh:1:dir:bf62f58e2c4682ffe329a3d763b6f1722c9b1464
origin badgerevision badge
swh:1:rev:66a79944ee4cd116b27bc1a69137276885461db8
origin badgesnapshot badge
swh:1:snp:7eb50f12cf990a0030724453139e994df238639f
Citations

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: 66a79944ee4cd116b27bc1a69137276885461db8 authored by Andrew Litteken on 28 September 2021, 15:30:02 UTC
Merge pull request #49 from AndrewLitteken/master
Tip revision: 66a7994
square_root.n10.scaffold
/*****************************************************************************
 *
 * Grover's Search Algorithm
 *
 * Implements Grover's Search algorithm in the Scaffold programming language
 *
 * February 2013
 *
 *****************************************************************************/

#include <math.h>

#define n 10 // problem size (log of database size)

#define pi 3.141592653589793238462643383279502884197

// Module prototypes
void diffuse(qbit q[n]);
void Sqr(qbit a[n], qbit b[n]);
void EQxMark(qbit b[n], qbit t[1], int tF);


/***************************
 Diffusion Module
***************************/
void diffuse(qbit q[n]) {
  int j;
  // local qbit x[n-1];  // scratch register
  // No local registers yet
  qbit x[n-1];  // scratch register

  // Hadamard applied to q
  // No forall yet
  // forall(j=0; j<n; j++) { H(q[j]); }
  for(j=0; j<n; j++) { H(q[j]); }

  // Want to phase flip on q = 00...0
  // So invert q to compute q[0] and q[1] and ... and q[n-1]
  for(j = 0; j < n; j++) 
    X(q[j]);  

  // Compute x[n-2] = q[0] and q[1] and ... and q[n-1]
  // No forall yet
  // forall(j=0; j<n-1; j++) PrepZ(x[j]);
  for(j=0; j<n-1; j++) PrepZ(x[j],0);
  Toffoli(x[0], q[1], q[0]); 
  for(j = 1; j < n-1; j++)
    Toffoli(x[j], x[j-1], q[j+1]);
  
  // Phase flip conditioned on x[n-2]
  Z(x[n-2]);  // Phase Flip==Z if q=00...0, i.e. x[n-2]==1

  // Undo the local registers
  for(j = n-2; j > 0; j--)
    Toffoli(x[j], x[j-1], q[j+1]);
  Toffoli(x[0], q[1], q[0]); 
   
  // Restore q
  for(j = 0; j < n; j++) 
    X(q[j]);
        
  // Complete the diffusion
  // No forall yet
  // forall(j=0; j<n; j++) { H(q[j]); }
  for(j=0; j<n; j++) { H(q[j]); }
}

/***********************************************
 Test if the input polynomial
   b(x) = b0 + b1*x + ... + b_(n-1)*x^(n-1) == x
 over the ring GF(2)[x] mod (x^n + x + 1).


 if(tF!=0) set return result in qubit t[0] else Z
************************************************/
void EQxMark(qbit b[n], qbit t[1], int tF) {
  int j;
  // No local registers yet
  // local qbit x[n-1];  // scratch register
  qbit x[n-1];  // scratch register

  // Change b to reflect testing for the polynomial x
  for(j = 0; j < n; j++) 
    if(j!=1) X(b[j]);         

  // Compute x[n-2] = b[0] and b[1] and ... and b[n-1]
  // No forall yet
  // forall(j=0; j<n-1; j++) PrepZ(x[j]);
  for(j=0; j<n-1; j++) PrepZ(x[j],0);
  Toffoli(x[0], b[1], b[0]); 
  for(j = 1; j < n-1; j++)
      Toffoli(x[j], x[j-1], b[j+1]);
  
  // Either return result in t or Phase flip conditioned on x[n-2]
  if(tF!=0) {
    CNOT(t[0], x[n-2]); // Returns result in t
  }
  else {
    Z(x[n-2]);  // Phase Flip==Z if b=01...0 == 'x', i.e. x[n-2]==1
  }

  // Undo the local registers
  for(j = n-2; j > 0; j--)
    Toffoli(x[j], x[j-1], b[j+1]);
  Toffoli(x[0], b[1], b[0]); 
   
  // Restore b
  for(j = 0; j < n; j++) 
    if(j!=1) X(b[j]);       
} 

/***********************************************
 Squaring a(x) = a0 + a1*x + ... + a_(n-1)*x^(n-1)
 over the ring GF(2)[x] mod (x^n + x + 1).

 Result placed in the n-qubit register b
************************************************/
void Sqr(qbit a[n], qbit b[n]) {

  int i;
  int k;
  
  // Using forall indicates the CNOT's are independent
  // So these instructions can be done in parallel
  // No forall yet
  // forall(i=0; i<=(n-1)/2; i++) {
  for(i=0; i<=(n-1)/2; i++) {
    k = 2*i;
    CNOT(b[k], a[i]); 
  }

  // Would it make a difference to split this into two loops?
  // Maybe since deleaving would be two forall loops!
  // Or perhaps it is okay to just replace the for with a forall.
  for(i=(n+1)/2; i<n; i++) {
    k = (2*i)-n;
    CNOT(b[k],   a[i]); 
    CNOT(b[k+1], a[i]); 
  }
}

/***************************************************
 Program to compute the sqrt(x) in the polynomial ring

     GF(2)[x] / (x^n+x+1)

 Elements of the ring are represented as polynomials
 of maximum degree n-1:

   a(x) = a[0] + a[1]*x + ... + a[n-1]*x^(n-1)

 Use grover search.

****************************************************/
int main() { 

  // Quantum registers
  qbit a[n];  // Polynomials to search
  qbit b[n];  // b(x) = a(x)*a(x) mod (x^n + x + 1)
  qbit t[1];  // Test result if b(x) == x

  // Grover parameters and step index
  int N= pow(2,n);
  int nstep = floor((pi/4)*sqrt(N));
  int istep;

  // Holds final measurement values
  cbit mt[1]; // measure t
  cbit ma[n]; // measure a  This holds the square root
  cbit mb[n]; // measure b  No need to measure since b(x) should be x
  int i;

  // Initialize a[0..n-1] into uniform superposition
  // No forall yet
  // forall(i=0; i<n; i++) H(a[i]);
  for(i=0; i<n; i++) H(a[i]);

  // Grover iteration: Mark then diffuse
  for(istep=1; istep<=nstep; istep++) {
    
    Sqr(a, b);         // Sets b(x) = a(x) * a(x)
    EQxMark(b, t, 0);  // Tests if b(x) == x and if so Phase Flips
    Sqr(a, b);         // Note: Sqr is it's own inverse
      
    // Diffuse
    diffuse(a);
  }
  
  // For the final measurement, compute causal state
  Sqr(a, b);
  EQxMark(b, t, 1);   // Note; 1 implies test result b(x)==x is returned in t

  // Now measure and report
  mt[0] = MeasZ(t[0]);  // If mt[0]==1 then success
  // measure a to recover the square root
  for(i=0; i<n; i++) ma[i] = MeasZ(a[i]);

    return 0;
}

// Problem instances
//main(2);
//main(3);
//main(5);
//main(8);
//main(13);
//main(19);

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Contact— JavaScript license information— Web API

back to top