Ejemplo n.º 1
0
        private ObservableCollection <GateVM> CreateGatesFromModel()
        {
            ObservableCollection <GateVM> gates = new ObservableCollection <GateVM>();

            for (int i = 0; i < _model.Steps[_column].Gates.Count; i++)
            {
                Gate             gate    = _model.Steps[_column].Gates[i];
                RegisterRefModel gateRow = _model.GetRefFromOffset(i);
                gates.Add(new GateVM(_model, gateRow, _column));
            }
            return(gates);
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        public void CNot(int target, int control)
        {
            Validate(target, "CNot", "target");
            Validate(control, "CNot", "control");

            RegisterRefModel controlRef, targetRef;

            if (_model == null)
            {
                controlRef = _compModel.GetRefFromOffset(control);
                targetRef  = _compModel.GetRefFromOffset(target);
            }
            else
            {
                controlRef = new RegisterRefModel()
                {
                    Register = _model, Offset = _offsetToModel + control
                };
                targetRef = new RegisterRefModel()
                {
                    Register = _model, Offset = _offsetToModel + target
                };
            }
            CNotGate gate = new CNotGate(targetRef, controlRef);

            AddGate(gate);
        }