Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using API.Interfaces;
using API.DummyClasses;
using API.Problems.NPComplete.NPC_BERNSTEINVAZIRANI.Solvers;
using API.Problems.NPComplete.NPC_BERNSTEINVAZIRANI.Verifiers;
using SPADE;
Expand All @@ -11,13 +10,13 @@ class BERNSTEINVAZIRANI : IProblem<BernsteinVaziraniClassicalSolver, BernsteinVa
{

// --- Fields ---
public string problemName {get;} = "Bernstein Vazirani"; // Name as it appears in the dropdown selection panel
public string problemLink {get;} = "https://en.wikipedia.org/wiki/Bernstein%E2%80%93Vazirani_algorithm"; // Link to the Wikipedia page for the problem
public string formalDefinition {get;} = "Bernstein Vazirani = {(n, <w_1, w_2, ... , w_(2^n - 1), w_(2^n)> | n is int, w_i is bit (0 or 1)}"; // Mathematical description of the problem (todo later)
public string problemDefinition { get; } = "The Bernstein-Vazirani problem asks for the identification of an unknown bit string s that defines a linear Boolean function f(x)= s*x (mod 2). The task is to determine the hidden string s using as few queries as possible"; // plaintext description of the problem
public string source { get; } = "Bernstein, Ethan, and Umesh, Vazirani. Quantum complexity theory. Proceedings of the twenty-fifth annual ACM symposium on Theory of computing. 1993."; // Academic paper proper citation
public string sourceLink { get; } = "https://dl.acm.org/doi/pdf/10.1145/167088.167097"; // Link to the academic paper
private static readonly string _defaultInstance = "(3,(0,1,0,1,1,0,1,0))";
public string problemName { get; } = "Bernstein Vazirani";
public string problemLink { get; } = "https://en.wikipedia.org/wiki/Bernstein%E2%80%93Vazirani_algorithm";
public string formalDefinition { get; } = "Bernstein Vazirani = {(n, <w_1, w_2, ... , w_(2^n - 1), w_(2^n)> | n is int, w_i is bit (0 or 1)}";
public string problemDefinition { get; } = "The Bernstein-Vazirani problem asks for the identification of an unknown bit string s that defines a linear Boolean function f(x)= s*x (mod 2). The task is to determine the hidden string s using as few queries as possible";
public string source { get; } = "Bernstein, Ethan, and Umesh, Vazirani. Quantum complexity theory. Proceedings of the twenty-fifth annual ACM symposium on Theory of computing. 1993.";
public string sourceLink { get; } = "https://dl.acm.org/doi/pdf/10.1145/167088.167097";
private static readonly string _defaultInstance = "(0,1,0,1,1,0,1,0)";
public string defaultInstance {get;} = _defaultInstance;
public string instance {get;set;} = string.Empty;
public string wikiName {get;} = ""; // Wiki name or link? - not used yet
Expand Down Expand Up @@ -51,6 +50,13 @@ public int NBits {
}
}

static public int PowerOfTwo(int n)
{
if (n > 0 && (n & (n - 1)) == 0)
return (int)Math.Log2(n);
throw new ArithmeticException("not a power of two");
}

public bool Func(int x)
{
if (x < 0 || x >= funcValues.Count) {
Expand All @@ -64,11 +70,10 @@ public bool Func(int x)
public BERNSTEINVAZIRANI(string input) {
instance = input;

StringParser parser = new("{(n, S) | n is int, S is list}");
StringParser parser = new("{f | f is list}");
parser.parse(instance);

int n = int.Parse(parser["n"].ToString());
UtilCollection bitslist = parser["S"];
UtilCollection bitslist = parser["f"];

var fvalues = new List<bool>();
bitslist.ToList().ForEach(x => {
Expand All @@ -78,17 +83,10 @@ public BERNSTEINVAZIRANI(string input) {
fvalues.Add(true);
} else {
Console.WriteLine($"{this.GetType().Name}:{MethodBase.GetCurrentMethod()?.Name}: encountered non-bit value {x.ToString()} in function values list");
// XXX how do deal with error?
}
});

if (fvalues.Count != Math.Pow(2, n)) {
Console.WriteLine($"{this.GetType().Name}:{MethodBase.GetCurrentMethod()?.Name}: wrong length of function value list (should be {Math.Pow(2, n)}, got {fvalues.Count})");
// XXX the array is the wrong size... how to deal with error?
return;
}

nbits = n;
nbits = PowerOfTwo(fvalues.Count);
funcValues = fvalues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ public class BERNSTEINVAZIRANI_tests {
[Fact]
public void BERNSTEINVAZIRANI_Default_Instantiation() {
var problem = new BERNSTEINVAZIRANI();
Assert.Equal("(3,(0,1,0,1,1,0,1,0))", problem.instance);
Assert.Equal("(3,(0,1,0,1,1,0,1,0))", problem.defaultInstance);
Assert.Equal("(0,1,0,1,1,0,1,0)", problem.instance);
Assert.Equal("(0,1,0,1,1,0,1,0)", problem.defaultInstance);
}

[Fact]
public void BERNSTEINVAZIRANI_Custom_Instantiation() {
string instance = "(3,(0,1,0,1,0,1,0,1))";
string instance = "(0,1,0,1,0,1,0,1)";
var problem = new BERNSTEINVAZIRANI(instance);
Assert.Equal(instance, problem.instance);
}

[Theory] //tests verifier
[InlineData("(3,(0,1,0,1,1,0,1,0))", "101", true)]
[InlineData("(3,(0,1,1,0,1,0,0,1))", "111", true)]
[InlineData("(3,(0,1,0,1,0,1,0,1))", "001", true)]
[InlineData("(3,(0,0,0,0,0,0,0,0))", "000", true)]
[InlineData("(0,1,0,1,1,0,1,0)", "101", true)]
[InlineData("(0,1,1,0,1,0,0,1)", "111", true)]
[InlineData("(0,1,0,1,0,1,0,1)", "001", true)]
[InlineData("(0,0,0,0,0,0,0,0)", "000", true)]
public void BERNSTEINVAZIRANI_verifier(string instance, string certificate, bool expected) {
var problem = new BERNSTEINVAZIRANI(instance);
var verifier = new BernsteinVaziraniClassicalVerifier();
Expand All @@ -34,10 +34,10 @@ public void BERNSTEINVAZIRANI_verifier(string instance, string certificate, bool
}

[Theory] //tests solver
[InlineData("(3,(0,1,0,1,1,0,1,0))", "101")]
[InlineData("(3,(0,1,1,0,1,0,0,1))", "111")]
[InlineData("(3,(0,0,0,0,0,0,0,0))", "000")]
[InlineData("(3,(0,1,0,1,0,1,0,1))", "001")]
[InlineData("(0,1,0,1,1,0,1,0)", "101")]
[InlineData("(0,1,1,0,1,0,0,1)", "111")]
[InlineData("(0,0,0,0,0,0,0,0)", "000")]
[InlineData("(0,1,0,1,0,1,0,1)", "001")]
public void BERNSTEINVAZIRANI_solver(string instance, string certificate) {
var problem = new BERNSTEINVAZIRANI(instance);
var solver = problem.defaultSolver;
Expand Down