public StepVM(ComputerModel model, int column) { _model = model; _column = column; _model.Steps[_column].Gates.CollectionChanged += Gates_CollectionChanged; _gates = CreateGatesFromModel(); }
public GateVM(ComputerModel model, RegisterRefModel row, int column) { _model = model; _row = row; _column = column; _model.StepChanged += _model_CurrentStepChanged; }
public RegisterVM(ComputerModel model, int registerIndex) { _model = model; _registerIndex = registerIndex; _model.Registers[_registerIndex].Qubits.CollectionChanged += Qubits_CollectionChanged; _model.StepChanged += _model_StepChanged; }
internal Register(ComputerModel compModel, Quantum.Register reg, RegisterModel regModel, int offsetToModel = 0) { _compModel = compModel; _register = reg; _model = regModel; _offsetToModel = offsetToModel; }
public QubitVM(ComputerModel model, int registerIndex, int rowIndex) { _model = model; _registerIndex = registerIndex; _rowIndex = rowIndex; _model.StepChanged += _model_CurrentStepChanged; _deleteRegister = new DelegateCommand(DeleteRegister, x => model.Registers.Count > 1); }
public static ComputerModel CreateModelForParser() { ComputerModel toReturn = new ComputerModel(); toReturn._registers = new ObservableCollection <RegisterModel>(); toReturn._steps = new ObservableCollection <StepModel>(); return(toReturn); }
public static ComputerModel CreateModelForGUI() { ComputerModel toReturn = new ComputerModel(); toReturn.CreateRegisters(); toReturn.CreateSteps(); return(toReturn); }
public CircuitGridVM(ComputerModel model) { _model = model; _registers = CreateRegistersFromModel(); _steps = CreateStepsFromModel(); _model.Steps.CollectionChanged += Steps_CollectionChanged; _model.Registers.CollectionChanged += Registers_CollectionChanged; _model.StepChanged += CurrentStepChanged; _model.SelectionChanged += _model_SelectionChanged; }
private void InitFromModel(ComputerModel model) { if (_model != null) { Dictionary<string, List<Gate>> oldComposites = _model.CompositeGates; Dictionary<string, List<Gate>> newComposites = model.CompositeGates; foreach (var pair in oldComposites) { if (!newComposites.ContainsKey(pair.Key)) { newComposites[pair.Key] = pair.Value; } } } _model = model; CircuitGrid = new CircuitGridVM(_model); CircuitEvaluator eval = CircuitEvaluator.GetInstance(); _outputModel = eval.InitFromModel(_model); OutputGrid.LoadModel(_model, _outputModel); }
private string GenerateStepCode(ComputerModel model, StepModel step, int column, string indent) { StringBuilder builder = new StringBuilder(); string gateString; Gate lastGate = null; foreach (Gate gate in step.Gates) { if (gate != lastGate) { gateString = GenerateGateCode(model, gate, column, indent); if (gateString.Length > 0) { builder.Append(indent).AppendLine(gateString); } } lastGate = gate; } return builder.ToString(); }
private string GenerateGateCode(ComputerModel model, Gate gate, int column, string indent, string defaultRegName = null) { Func<string, Gate, string> appendControlTarget = (string begin, Gate g) => { if (g.Control.HasValue) { string namedC = "control: "; string namedT = "target: "; if (g.Name != GateName.CNot) { namedC = ""; namedT = ""; } if (g.Control.Value.Register == g.Target.Register) { return String.Format("{0}.{1}{4}{2}, {5}{3});", GetRegName(g.Target.Register, defaultRegName), begin, g.Target.Offset, g.Control.Value.Offset, namedT, namedC); } else { return String.Format("comp.{0}{5}{1}[{2}], {6}{3}[{4}]);", begin, GetRegName(g.Target.Register, defaultRegName), g.Target.Offset, GetRegName(g.Control.Value.Register, defaultRegName), g.Control.Value.Offset, namedT, namedC); } } else { return String.Format("{0}.{1}{2});", GetRegName(g.Target.Register, defaultRegName), begin, g.Target.Offset); } }; Func<string, string, Gate, RegisterRefModel[], string> appendMoreControls = (string begin, string beginComment, Gate g, RegisterRefModel[] controls) => { if (controls.All<RegisterRefModel>(x => x.Register == g.Target.Register) && (g.Control == null || g.Control.Value.Register == g.Target.Register)) { StringBuilder format = new StringBuilder(GetRegName(g.Target.Register, defaultRegName)).Append(".").Append(begin); format.Append(g.Target.Offset); if (g.Control.HasValue) { format.Append(", ").Append(g.Control.Value.Offset); } for (int i = 0; i < controls.Length; i++) { format.Append(", ").Append(controls[i].Offset); } format.Append(");"); format.Append("\t\t// ").Append(beginComment).Append("<target_bit>, ... <control_bits> ...)"); return format.ToString(); } else { StringBuilder format = new StringBuilder("comp.").Append(begin); format.Append(GetRegName(g.Target.Register, defaultRegName)) .Append("[").Append(g.Target.Offset).Append("]"); if (g.Control.HasValue) { format.Append(", ") .Append(GetRegName(g.Control.Value.Register, defaultRegName)).Append("[") .Append(g.Control.Value.Offset).Append("]"); } for (int i = 0; i < controls.Length; i++) { format.Append(", ") .Append(GetRegName(controls[i].Register, defaultRegName)).Append("[") .Append(controls[i].Offset).Append("]"); } format.Append(");"); format.Append("\t\t// ").Append(beginComment).Append("<target_bit>, ... <control_bits> ...)"); return format.ToString(); } }; switch (gate.Name) { case GateName.Hadamard: return appendControlTarget("Hadamard(", gate); case GateName.SigmaX: return appendControlTarget("SigmaX(", gate); case GateName.SigmaY: return appendControlTarget("SigmaY(", gate); case GateName.SigmaZ: return appendControlTarget("SigmaZ(", gate); case GateName.SqrtX: return appendControlTarget("SqrtX(", gate); case GateName.RotateX: RotateXGate rx = gate as RotateXGate; return appendControlTarget(String.Format("RotateX({0}, ", rx.Gamma), gate); case GateName.RotateY: RotateYGate ry = gate as RotateYGate; return appendControlTarget(String.Format("RotateY({0}, ", ry.Gamma), gate); case GateName.RotateZ: RotateZGate rz = gate as RotateZGate; return appendControlTarget(String.Format("RotateZ({0}, ", rz.Gamma), gate); case GateName.PhaseKick: PhaseKickGate pk = gate as PhaseKickGate; if (pk.Controls.Length > 0) { return appendMoreControls( String.Format("PhaseKick({0}, ", pk.Gamma), "PhaseKick(<gamma_value>, ", gate, pk.Controls); } else { return appendControlTarget(String.Format("PhaseKick({0}, ", pk.Gamma), gate); } case GateName.PhaseScale: PhaseScaleGate ps = gate as PhaseScaleGate; return appendControlTarget(String.Format("PhaseScale({0}, ", ps.Gamma), gate); case GateName.CNot: return appendControlTarget("CNot(", gate); case GateName.CPhaseShift: CPhaseShiftGate cps = gate as CPhaseShiftGate; if (cps.Controls.Length > 0) { return appendMoreControls( String.Format("CPhaseShift({0}, ", cps.Dist), "CPhaseShift(<phase_distance_value>, ", gate, cps.Controls); } else { return appendControlTarget(String.Format("CPhaseShift({0}, ", cps.Dist), gate); } case GateName.InvCPhaseShift: InvCPhaseShiftGate icps = gate as InvCPhaseShiftGate; if (icps.Controls.Length > 0) { return appendMoreControls( String.Format("InverseCPhaseShift({0}, ", icps.Dist), "InverseCPhaseShift(<phase_distance_value>, ", gate, icps.Controls); } else { return appendControlTarget(String.Format("InverseCPhaseShift({0}, ", icps.Dist), gate); } case GateName.Toffoli: ToffoliGate t = gate as ToffoliGate; return appendMoreControls( "Toffoli(", "Toffoli(", gate, t.Controls); case GateName.Measure: MeasureGate m = gate as MeasureGate; if (m.Begin == m.End) { return String.Format("{0}.Measure({1});", GetRegName(m.Target.Register, defaultRegName), m.Target.Offset); } else if (m.BeginRow.Register == m.EndRow.Register) { string regName = GetRegName(m.BeginRow.Register, defaultRegName); if (m.BeginRow.Register != null) { if (m.End - m.Begin + 1 == m.BeginRow.Register.Qubits.Count) { return String.Format("{0}.Measure();", regName); } else { StringBuilder builder = new StringBuilder(); for (int i = m.BeginRow.Offset; i <= m.EndRow.Offset; i++) { builder.AppendFormat("{0}.Measure({1});\n", regName, i); } return builder.ToString(); } } else { if (m.End - m.Begin + 1 == model.TotalWidth) { return String.Format("{0}.Measure();", regName); } else { StringBuilder builder = new StringBuilder(); for (int i = m.BeginRow.Offset; i <= m.EndRow.Offset; i++) { builder.AppendFormat("{0}.Measure({1});\n", regName, i); } return builder.ToString(); } } } else { StringBuilder builder = new StringBuilder(); int i = m.Begin; while (i <= m.End) { RegisterRefModel regRef = model.GetRefFromOffset(i); if (i + regRef.Register.Qubits.Count < m.End + 2) { builder.AppendFormat("{0}.Measure();\n", regRef.Register.Name); i += regRef.Register.Qubits.Count; } else { builder.AppendFormat("{0}.Measure({1});\n", regRef.Register.Name, regRef.Offset); i++; } } return builder.ToString(); } case GateName.Unitary: UnitaryGate u = gate as UnitaryGate; string uMatrixName = "unitary_" + column + "_" + u.Target.OffsetToRoot; string matrixDef = GenerateMatrixDefinition(uMatrixName, u.Matrix, indent); string gateDef = appendControlTarget(String.Format("Gate1({0}, ", uMatrixName), gate); return String.Format("{0}\n{1}{2}", matrixDef, indent, gateDef); case GateName.Parametric: return GenerateParametricGateCode(gate as ParametricGate, defaultRegName); case GateName.Composite: return GenerateCompositeGateCode(gate as CompositeGate, defaultRegName); case GateName.Empty: default: return String.Empty; } }
public string GenerateCompositeExtensions(ComputerModel model, Dictionary<string, List<MethodCode>> methods) { if ((methods != null && methods.Count > 0) || model.CompositeGates.Count > 0) { StringBuilder builder = new StringBuilder(); builder.AppendLine("\tpublic static class CompositeExtension"); builder.AppendLine("\t{"); if (methods != null) { foreach (List<MethodCode> list in methods.Values) { foreach (MethodCode code in list) { builder.Append("\t\t").AppendLine(code.Code); } } } foreach (var pair in model.CompositeGates) { if (methods == null || !methods.ContainsKey(pair.Key)) { string regName = "regA"; builder.Append("\t\tpublic static void ").Append(pair.Key); builder.Append("(this QuantumComputer comp, Register "); builder.Append(regName).AppendLine(")"); builder.AppendLine("\t\t{"); string indent = "\t\t\t"; int column = 0; foreach (Gate gate in pair.Value) { string gateString = GenerateGateCode(model, gate, column, indent, regName); if (gateString.Length > 0) { builder.Append(indent).AppendLine(gateString); } column++; } builder.AppendLine("\t\t}"); builder.AppendLine(); } } builder.AppendLine("\t}"); return builder.ToString(); } else { return null; } }
public static ComputerModel CreateModelForParser() { ComputerModel toReturn = new ComputerModel(); toReturn._registers = new ObservableCollection<RegisterModel>(); toReturn._steps = new ObservableCollection<StepModel>(); return toReturn; }
public static ComputerModel CreateModelForGUI() { ComputerModel toReturn = new ComputerModel(); toReturn.CreateRegisters(); toReturn.CreateSteps(); return toReturn; }
public Output InitFromModel(ComputerModel model) { return _comp.InitFromModel(model); }
public void LoadModel(ComputerModel model, Output outputModel) { _model = model; _outputModel = outputModel; _outputModel.OutputChanged += _model_OutputChanged; string[] regNames = new string[_model.Registers.Count + 1]; regNames[0] = "root"; for (int i = 1; i < regNames.Length; i++) { regNames[i] = _model.Registers[i - 1].Name; } RegistersNames = regNames; }