Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version update and resource estimation fixes #2

Merged
merged 3 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 122 additions & 89 deletions aes/BoyarPeralta12.qs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BoyarPeralta11
open Microsoft.Quantum.Intrinsic;
open QUtilities;

operation ForwardSBox(u: Qubit[], s: Qubit[], t: Qubit[], m: Qubit[], l: Qubit[], costing: Bool) : Unit
operation ForwardSBox(u: Qubit[], s: Qubit[], t: Qubit[], m: Qubit[], l: Qubit[], anc: Qubit[], costing: Bool) : Unit
{
body (...)
{
Expand Down Expand Up @@ -39,69 +39,75 @@ namespace BoyarPeralta11
LPXOR(t[3-1], t[16-1], t[26-1]);
LPXOR(t[1-1], t[12-1], t[27-1]);

LPAND(t[13-1], t[6-1], m[1-1], costing);
LPAND(t[23-1], t[8-1], m[2-1], costing);
LPXOR(t[14-1], m[1-1], m[3-1]);
LPAND(t[19-1], u[7], m[4-1], costing);
LPXOR(m[4-1], m[1-1], m[5-1]);
LPAND(t[3-1], t[16-1], m[6-1], costing);
LPAND(t[22-1], t[9-1], m[7-1], costing);
LPXOR(t[26-1], m[6-1], m[8-1]);
LPAND(t[20-1], t[17-1], m[9-1], costing);
LPXOR(m[9-1], m[6-1], m[10-1]);
LPAND(t[1-1], t[15-1], m[11-1], costing);
LPAND(t[4-1], t[27-1], m[12-1], costing);
LPXOR(m[12-1], m[11-1], m[13-1]);
LPAND(t[2-1], t[10-1], m[14-1], costing);
LPXOR(m[14-1], m[11-1], m[15-1]);
LPXOR(m[3-1], m[2-1], m[16-1]);
LPXOR(m[5-1], t[24-1], m[17-1]);
LPXOR(m[8-1], m[7-1], m[18-1]);
LPXOR(m[10-1], m[15-1], m[19-1]);
LPXOR(m[16-1], m[13-1], m[20-1]);
LPXOR(m[17-1], m[15-1], m[21-1]);
LPXOR(m[18-1], m[13-1], m[22-1]);
LPXOR(m[19-1], t[25-1], m[23-1]);
LPXOR(m[22-1], m[23-1], m[24-1]);
LPAND(m[22-1], m[20-1], m[25-1], costing);
LPXOR(m[21-1], m[25-1], m[26-1]);
LPXOR(m[20-1], m[21-1], m[27-1]);
LPXOR(m[23-1], m[25-1], m[28-1]);
LPAND(m[28-1], m[27-1], m[29-1], costing);
LPAND(m[26-1], m[24-1], m[30-1], costing);
LPAND(m[20-1], m[23-1], m[31-1], costing);
LPAND(m[27-1], m[31-1], m[32-1], costing);
LPXOR(m[27-1], m[25-1], m[33-1]);
LPAND(m[21-1], m[22-1], m[34-1], costing);
LPAND(m[24-1], m[34-1], m[35-1], costing);
LPXOR(m[24-1], m[25-1], m[36-1]);
LPXOR(m[21-1], m[29-1], m[37-1]);
LPXOR(m[32-1], m[33-1], m[38-1]);
LPXOR(m[23-1], m[30-1], m[39-1]);
LPXOR(m[35-1], m[36-1], m[40-1]);
LPXOR(m[38-1], m[40-1], m[41-1]);
LPXOR(m[37-1], m[39-1], m[42-1]);
LPXOR(m[37-1], m[38-1], m[43-1]);
LPXOR(m[39-1], m[40-1], m[44-1]);
LPXOR(m[42-1], m[41-1], m[45-1]);
LPAND(m[44-1], t[6-1], m[46-1], costing);
LPAND(m[40-1], t[8-1], m[47-1], costing);
LPAND(m[39-1], u[7], m[48-1], costing);
LPAND(m[43-1], t[16-1], m[49-1], costing);
LPAND(m[38-1], t[9-1], m[50-1], costing);
LPAND(m[37-1], t[17-1], m[51-1], costing);
LPAND(m[42-1], t[15-1], m[52-1], costing);
LPAND(m[45-1], t[27-1], m[53-1], costing);
LPAND(m[41-1], t[10-1], m[54-1], costing);
LPAND(m[44-1], t[13-1], m[55-1], costing);
LPAND(m[40-1], t[23-1], m[56-1], costing);
LPAND(m[39-1], t[19-1], m[57-1], costing);
LPAND(m[43-1], t[3-1], m[58-1], costing);
LPAND(m[38-1], t[22-1], m[59-1], costing);
LPAND(m[37-1], t[20-1], m[60-1], costing);
LPAND(m[42-1], t[1-1], m[61-1], costing);
LPAND(m[45-1], t[4-1], m[62-1], costing);
LPAND(m[41-1], t[2-1], m[63-1], costing);

LPANDWithAux(t[13-1], t[6-1], m[1-1], anc[0], costing); // depth 0->1
LPANDWithAux(t[23-1], t[8-1], m[2-1], anc[1], costing); // depth 0->1
LPANDWithAux(t[19-1], u[7], m[4-1], anc[2], costing); // depth 0->1
LPANDWithAux(t[3-1], t[16-1], m[6-1], anc[3], costing); // depth 0->1
LPANDWithAux(t[22-1], t[9-1], m[7-1], anc[4], costing); // depth 0->1
LPANDWithAux(t[20-1], t[17-1], m[9-1], anc[5], costing); // depth 0->1
LPANDWithAux(t[1-1], t[15-1], m[11-1], anc[6], costing); // depth 0->1
LPANDWithAux(t[4-1], t[27-1], m[12-1], anc[7], costing); // depth 0->1
LPANDWithAux(t[2-1], t[10-1], m[14-1], anc[8], costing); // depth 0->1

LPXOR(t[14-1], m[1-1], m[3-1]); // depth 1->1
LPXOR(m[4-1], m[1-1], m[5-1]); // depth 1->1
LPXOR(t[26-1], m[6-1], m[8-1]); // depth 1->1
LPXOR(m[9-1], m[6-1], m[10-1]); // depth 1->1
LPXOR(m[12-1], m[11-1], m[13-1]); // depth 1->1
LPXOR(m[14-1], m[11-1], m[15-1]); // depth 1->1
LPXOR(m[3-1], m[2-1], m[16-1]); // depth 1->1
LPXOR(m[5-1], t[24-1], m[17-1]); // depth 1->1
LPXOR(m[8-1], m[7-1], m[18-1]); // depth 1->1
LPXOR(m[10-1], m[15-1], m[19-1]); // depth 1->1
LPXOR(m[16-1], m[13-1], m[20-1]); // depth 1->1
LPXOR(m[17-1], m[15-1], m[21-1]); // depth 1->1
LPXOR(m[18-1], m[13-1], m[22-1]); // depth 1->1
LPXOR(m[19-1], t[25-1], m[23-1]); // depth 1->1
LPXOR(m[22-1], m[23-1], m[24-1]); // depth 1->1
LPANDWithAux(m[22-1], m[20-1], m[25-1], anc[0], costing); // depth 1->2

LPXOR(m[21-1], m[25-1], m[26-1]); // depth 2->2
LPXOR(m[20-1], m[21-1], m[27-1]); // depth 2->2
LPXOR(m[23-1], m[25-1], m[28-1]); // depth 2->2
LPANDWithAux(m[28-1], m[27-1], m[29-1], anc[0], costing); // depth 2->3
LPANDWithAux(m[26-1], m[24-1], m[30-1], anc[1], costing); // depth 2->3
LPANDWithAux(m[20-1], m[23-1], m[31-1], anc[2], costing); // depth 2->3
LPANDWithAux(m[21-1], m[22-1], m[34-1], anc[3], costing); // depth 2->3

LPANDWithAux(m[27-1], m[31-1], m[32-1], anc[0], costing); // depth 3->4
LPANDWithAux(m[24-1], m[34-1], m[35-1], anc[1], costing); // depth 3->4
LPXOR(m[21-1], m[29-1], m[37-1]); // depth 3->3
LPXOR(m[23-1], m[30-1], m[39-1]); // depth 3->3
LPXOR(m[37-1], m[39-1], m[42-1]); // depth 3->3

LPXOR(m[27-1], m[25-1], m[33-1]); // depth 4->4
LPXOR(m[24-1], m[25-1], m[36-1]); // depth 4->4
LPXOR(m[32-1], m[33-1], m[38-1]); // depth 4->4
LPXOR(m[35-1], m[36-1], m[40-1]); // depth 4->4
LPXOR(m[38-1], m[40-1], m[41-1]); // depth 4->4
LPXOR(m[37-1], m[38-1], m[43-1]); // depth 4->4
LPXOR(m[39-1], m[40-1], m[44-1]); // depth 4->4
LPXOR(m[42-1], m[41-1], m[45-1]); // depth 4->4
LPANDWithAux(m[44-1], t[6-1], m[46-1], anc[0], costing); // depth 4->5
LPANDWithAux(m[40-1], t[8-1], m[47-1], anc[1], costing); // depth 4->5
LPANDWithAux(m[39-1], u[7], m[48-1], anc[2], costing); // depth 4->5
LPANDWithAux(m[43-1], t[16-1], m[49-1], anc[3], costing); // depth 4->5
LPANDWithAux(m[38-1], t[9-1], m[50-1], anc[4], costing); // depth 4->5
LPANDWithAux(m[37-1], t[17-1], m[51-1], anc[5], costing); // depth 4->5
LPANDWithAux(m[42-1], t[15-1], m[52-1], anc[6], costing); // depth 4->5
LPANDWithAux(m[45-1], t[27-1], m[53-1], anc[7], costing); // depth 4->5
LPANDWithAux(m[41-1], t[10-1], m[54-1], anc[8], costing); // depth 4->5

LPANDWithAux(m[44-1], t[13-1], m[55-1], anc[0], costing); // depth 5->6
LPANDWithAux(m[40-1], t[23-1], m[56-1], anc[1], costing); // depth 5->6
LPANDWithAux(m[39-1], t[19-1], m[57-1], anc[2], costing); // depth 5->6
LPANDWithAux(m[43-1], t[3-1], m[58-1], anc[3], costing); // depth 5->6
LPANDWithAux(m[38-1], t[22-1], m[59-1], anc[4], costing); // depth 5->6
LPANDWithAux(m[37-1], t[20-1], m[60-1], anc[5], costing); // depth 5->6
LPANDWithAux(m[42-1], t[1-1], m[61-1], anc[6], costing); // depth 5->6
LPANDWithAux(m[45-1], t[4-1], m[62-1], anc[7], costing); // depth 5->6
LPANDWithAux(m[41-1], t[2-1], m[63-1], anc[8], costing); // depth 5->6

LPXOR(m[61-1], m[62-1], l[0]);
LPXOR(m[50-1], m[56-1], l[1]);
Expand Down Expand Up @@ -142,7 +148,7 @@ namespace BoyarPeralta11
{
body (...)
{
for (i in 1..n)
for i in 1..n
{
T(input[0]);
T(input[1]);
Expand Down Expand Up @@ -182,33 +188,60 @@ namespace BoyarPeralta11
adjoint auto;
}

operation SBox (input: Qubit[], output: Qubit[], costing: Bool) : Unit
function SBoxAncCount() : Int {
return 129;
}


// Wrapper function for an SBox
// Expects 129 qubits in ancAll, in the zeros state, and returns them to the zeros state
// If there are fewer qubits, it will allocate more as needed
operation SBox (input: Qubit[], output: Qubit[], ancAll: Qubit[], costing: Bool) : Unit {
body (...){
if Length(ancAll) < 129 {
let nExtra = 129 - Length(ancAll);
use extraAnc = Qubit[nExtra] {
_SBox(input, output, ancAll + extraAnc, costing);
}
} else {
_SBox(input, output, ancAll, costing);
}
}
adjoint auto;
}


// Internal Sbox
operation _SBox (input: Qubit[], output: Qubit[], ancAll: Qubit[], costing: Bool) : Unit
{
body (...)
{
using ((t, m, l) = (Qubit[27], Qubit[63], Qubit[30]))
{
let u = input[7..(-1)..0];
let s = output[7..(-1)..0];

ForwardSBox(u, s, t, m, l, costing);

// get out result
LPXOR(l[6], l[24], s[0]);
LPXNOR(l[16], l[26], s[1]);
LPXNOR(l[19], l[28], s[2]);
LPXOR(l[6], l[21], s[3]);
LPXOR(l[20], l[22], s[4]);
LPXOR(l[25], l[29], s[5]);
LPXNOR(l[13], l[27], s[6]);
LPXNOR(l[6], l[23], s[7]);

// uncompute
(Adjoint ForwardSBox)(u, s, t, m, l, costing);

// // dummy, forced-T-depth 2 s-box
// DummySBox(input, output, 2);
}
// Bookkeeping
let anc = Microsoft.Quantum.Arrays.Partitioned([27,63,30,9], ancAll);
let t = anc[0];
let m = anc[1];
let l = anc[2];
let ancAnd = anc[3];
let u = input[7..(-1)..0];
let s = output[7..(-1)..0];

ForwardSBox(u, s, t, m, l, ancAnd, costing);

// get out result
LPXOR(l[6], l[24], s[0]);
LPXNOR(l[16], l[26], s[1]);
LPXNOR(l[19], l[28], s[2]);
LPXOR(l[6], l[21], s[3]);
LPXOR(l[20], l[22], s[4]);
LPXOR(l[25], l[29], s[5]);
LPXNOR(l[13], l[27], s[6]);
LPXNOR(l[6], l[23], s[7]);

// uncompute
(Adjoint ForwardSBox)(u, s, t, m, l, ancAnd, costing);

// // dummy, forced-T-depth 2 s-box
// DummySBox(input, output, 2);
}
adjoint auto;
}
Expand Down
136 changes: 3 additions & 133 deletions aes/CSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,145 +2,15 @@
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Quantum.Simulation.Simulators.QCTraceSimulators;

using FileHelpers; // csv parsing

// Library that deals with making human-friendly the CSV tracer's output

namespace cs
namespace cswrapper
{
[DelimitedRecord("\t")]
[IgnoreFirst(1)]
public class DepthCounterCSV
{
public string Name;
public string Variant;
public string Caller;
public string CallerVariant;
public long Count;
[FieldConverter(typeof(QDecimalConverter))]
public decimal DepthAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal DepthSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal? DepthVariance;
public long DepthSum;
public long DepthMin;
public long DepthMax;
[FieldConverter(typeof(QDecimalConverter))]
public decimal StartTimeAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal StartTimeSecondMoment;
[FieldConverter(typeof(QDecimalConverter))] // [FieldConverter(ConverterKind.Decimal, ".")]
public decimal? StartTimeVariance;
public long StartTimeSum;
public long StartTimeMin;
public long StartTimeMax;
}

[DelimitedRecord("\t")]
[IgnoreFirst(1)]
public class WidthCounterCSV
{
public string Name;
public string Variant;
public string Caller;
public string CallerVariant;
public long Count;
[FieldConverter(typeof(QDecimalConverter))]
public decimal InputWidthAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal InputWidthSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal InputWidthVariance;
public long InputWidthSum;
public long InputWidthMin;
public long InputWidthMax;
[FieldConverter(typeof(QDecimalConverter))]
public decimal ExtraWidthAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal ExtraWidthSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal ExtraWidthVariance;
public long ExtraWidthSum;
public long ExtraWidthMin;
public long ExtraWidthMax;
[FieldConverter(typeof(QDecimalConverter))]
public decimal ReturnWidthAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal ReturnWidthSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal ReturnWidthVariance;
public long ReturnWidthSum;
public long ReturnWidthMin;
public long ReturnWidthMax;
[FieldConverter(typeof(QDecimalConverter))]
public decimal BorrowedWidthAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal BorrowedWidthSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal BorrowedWidthVariance;
public long BorrowedWidthSum;
public long BorrowedWidthMin;
public long BorrowedWidthMax;
}

[DelimitedRecord("\t")]
[IgnoreFirst(1)]
public class OperationCounterCSV
{
public string Name;
public string Variant;
public string Caller;
public string CallerVariant;
public long Count;
[FieldConverter(typeof(QDecimalConverter))]
public decimal CNOTAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal CNOTSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal CNOTVariance;
public long CNOTSum;
public long CNOTMin;
public long CNOTMax;
[FieldConverter(typeof(QDecimalConverter))]
public decimal QubitCliffordAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal QubitCliffordSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal QubitCliffordVariance;
public long QubitCliffordSum;
public long QubitCliffordMin;
public long QubitCliffordMax;
[FieldConverter(typeof(QDecimalConverter))]
public decimal RAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal RSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal RVariance;
public long RSum;
public long RMin;
public long RMax;
[FieldConverter(typeof(QDecimalConverter))]
public decimal MeasureAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal MeasureSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal MeasureVariance;
public long MeasureSum;
public long MeasureMin;
public long MeasureMax;
[FieldConverter(typeof(QDecimalConverter))]
public decimal TAverage;
[FieldConverter(typeof(QDecimalConverter))]
public decimal TSecondMoment;
[FieldConverter(typeof(QDecimalConverter))]
public decimal TVariance;
public long TSum;
public long TMin;
public long TMax;
}

public class QDecimalConverter : ConverterBase
{
Expand All @@ -152,7 +22,7 @@ public override object StringToField(string from)
}
else
{
return Decimal.Parse(from);
return Decimal.Parse(from, NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint);
}
}
public override string FieldToString(object fieldValue)
Expand Down
Loading