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 void SetGate(Gate gate) { int beginRow = gate.Begin; int endRow = gate.End; for (int i = beginRow; i <= endRow; i++) { _gates[i] = gate; } }
public void AddGate(Gate gate) { //StepModel currentStep = _steps.Count > 0 ? _steps[_steps.Count - 1] : null; //if (currentStep != null && currentStep.HasPlace(gate.Begin, gate.End)) //{ // currentStep.SetGate(gate); //} //else //{ if (_stepForNewGates == -1) { StepModel step = new StepModel(Registers); step.SetGate(gate); _steps.Add(step); } else { InsertStepRight(_stepForNewGates); _stepForNewGates++; _steps[_stepForNewGates].SetGate(gate); } //} }
private Gate PasteGate(Gate gate, int referenceBeginRow) { RegisterRefModel targetRef = GetRefFromOffset(gate.Target.OffsetToRoot + referenceBeginRow); RegisterRefModel? controlRef = null; if (gate.Control.HasValue) { controlRef = GetRefFromOffset(gate.Control.Value.OffsetToRoot + referenceBeginRow); } switch (gate.Name) { case GateName.CNot: return new CNotGate(targetRef, controlRef.Value); case GateName.Parametric: return PasteParametricGate(gate as ParametricGate, referenceBeginRow); case GateName.Composite: CompositeGate cg = gate as CompositeGate; RegisterRefModel partEndRef = GetRefFromOffset(cg.TargetRegister.EndOffsetToRoot + referenceBeginRow); if (partEndRef.Register.OffsetToRoot == targetRef.Register.OffsetToRoot) { RegisterPartModel target = new RegisterPartModel() { Register = targetRef.Register, Offset = targetRef.Offset, Width = cg.TargetRegister.Width }; return new CompositeGate(cg.FunctionName, target); } else { RegisterPartModel target = new RegisterPartModel() { Register = null, Offset = cg.TargetRegister.Offset + referenceBeginRow, Width = cg.TargetRegister.Width }; return new CompositeGate(cg.FunctionName, target); } case GateName.CPhaseShift: CPhaseShiftGate cps = gate as CPhaseShiftGate; RegisterRefModel[] controls = cps.Controls.Select<RegisterRefModel, RegisterRefModel>(x => GetRefFromOffset(x.OffsetToRoot + referenceBeginRow)).ToArray<RegisterRefModel>(); return new CPhaseShiftGate(cps.Dist, targetRef, controls); case GateName.Empty: return new EmptyGate(targetRef); case GateName.Hadamard: return new HadamardGate(targetRef, controlRef); case GateName.InvCPhaseShift: InvCPhaseShiftGate icps = gate as InvCPhaseShiftGate; RegisterRefModel[] icontrols = icps.Controls.Select<RegisterRefModel, RegisterRefModel>(x => GetRefFromOffset(x.OffsetToRoot + referenceBeginRow)).ToArray<RegisterRefModel>(); return new InvCPhaseShiftGate(icps.Dist, targetRef, icontrols); case GateName.Measure: MeasureGate mg = gate as MeasureGate; RegisterRefModel beginRef = GetRefFromOffset(mg.Begin + referenceBeginRow); RegisterRefModel endRef = GetRefFromOffset(mg.End + referenceBeginRow); return new MeasureGate(beginRef, endRef); case GateName.PhaseKick: PhaseKickGate pk = gate as PhaseKickGate; RegisterRefModel[] controls1 = pk.Controls.Select<RegisterRefModel, RegisterRefModel>(x => GetRefFromOffset(x.OffsetToRoot + referenceBeginRow)).ToArray<RegisterRefModel>(); return new PhaseKickGate(pk.Gamma, targetRef, controls1); case GateName.PhaseScale: PhaseScaleGate ps = gate as PhaseScaleGate; return new PhaseScaleGate(ps.Gamma, targetRef, controlRef); case GateName.RotateX: RotateXGate rx = gate as RotateXGate; return new RotateXGate(rx.Gamma, targetRef, controlRef); case GateName.RotateY: RotateYGate ry = gate as RotateYGate; return new RotateYGate(ry.Gamma, targetRef, controlRef); case GateName.RotateZ: RotateZGate rz = gate as RotateZGate; return new RotateZGate(rz.Gamma, targetRef, controlRef); case GateName.SigmaX: return new SigmaXGate(targetRef); case GateName.SigmaY: return new SigmaYGate(targetRef, controlRef); case GateName.SigmaZ: return new SigmaZGate(targetRef, controlRef); case GateName.SqrtX: return new SqrtXGate(targetRef, controlRef); case GateName.Toffoli: ToffoliGate t = gate as ToffoliGate; RegisterRefModel[] tcontrols = t.Controls.Select<RegisterRefModel, RegisterRefModel>(x => GetRefFromOffset(x.OffsetToRoot + referenceBeginRow)).ToArray<RegisterRefModel>(); return new ToffoliGate(targetRef, tcontrols); case GateName.Unitary: UnitaryGate u = gate as UnitaryGate; return new UnitaryGate(u.Matrix, targetRef, controlRef); default: return null; } }
private void AddGate(Gate gate) { QuantumComputer comp = QuantumComputer.GetInstance(); comp.AddGate(gate); }