using Quantum;
using Quantum.Operations;
using System;
using System.Collections.Generic;
using System.Numerics;
// any namespace name
namespace QuantumConsole
{
// there can be declared more classes
// any class name
public class QuantumTest
{
// it is possible to declare more custom methods
// there must be an entry point - Main method with signature as follows
// such entry point must be exactly one in whole file
public static void Main()
{
// for doing any computation, the QuantumComputer instance is needed
QuantumComputer comp = QuantumComputer.GetInstance();
// how to create register with initial value 0 and 2 qubits (|00>)
Register x = comp.NewRegister(0,2);
// it is also possible to create register in nontrivial state:
// firstly, we declare states and their amplitudes
// their square magnitudes (probabilities of possible states) should sum up to one
Dictionary initStates = new Dictionary() {
{ 0, 1.0 / Math.Sqrt(3) },
{ 1, 1.0 / Math.Sqrt(3) },
{ 2, 1.0 / Math.Sqrt(3) } };
//then we create register, giving these states and the width (number of qubits)
Register y = comp.NewRegister(initStates, 2);
// now we can perform computations
// e.g. Rotation around y-axis by the angle PI/4 on qubit number 0 (least significant bit)
x.RotateY(Math.PI / 4, 0);
// or totally custom unitary gate
// to do that, we firstly declare matrix of complex numbers:
Complex[,] w = new Complex[2,2];
w[0, 0] = Math.Sqrt(2.0/3.0);
w[0, 1] = -Math.Sqrt(1.0/3.0);
w[1, 0] = Math.Sqrt(1.0/3.0);
w[1, 1] = Math.Sqrt(2.0/3.0);
// and then we apply it to qubit number 1 (most significant bit in 2-qubit register x)
x.Gate1(w, 1);
// we can print register
Console.WriteLine(x);
// or obtain its amplitudes - it is a dictionary, like in creating register
IDictionary amplitudes = x.GetAmplitudes();
// or probabilities
IDictionary probs = x.GetProbabilities();
// or representing vector
Complex[] vector = x.GetVector();
// the computations on more that one register are also possible
// here we apply Toffoli gate where target qubit is y[0] - qubit number 0 in register y
// and control qubits are x[0] and x[1]
comp.Toffoli(y[0], x[0], x[1]);
// now printing and getting probabilities is still possible:
Console.WriteLine(x);
// but x.GetAmplitudes() and x.GetVector() return null
// that is because register x is no longer independent
// - it participated in multi-register operation and was during it connected with register y
// but we can obtain amplitudes and vector from root register, which contains connected registers x and y:
Register root = comp.GetRootRegister(x);
amplitudes = root.GetAmplitudes();
Console.WriteLine("The number of possible states of root register equals: {0}", amplitudes.Count);
// we can measure single qubit
Console.WriteLine("The result of measurement: {0}", y.Measure(0));
// or whole register:
ulong measuredX = x.Measure();
// if we want now to perform memory intensive computations,
// we can delete registers
// it is always preceded by measurement, what can be seen in generated circuit
comp.DeleteRegister(ref x);
// now x is null:
Console.WriteLine(x);
// however, deletion will be done automatically after end of program
// there is no need for deleting all registers just before end
}
}
}