Esempio n. 1
0
        public void SetGate(int pressedColumn, RegisterRefModel pressedRow, DragDropKeyStates keyStates)
        {
            ActionName action = HomeVM.SelectedAction;
            Gate oldGate;

            // make selection
            if (keyStates.HasFlag(DragDropKeyStates.ShiftKey))
            {
                // move selection
                if (keyStates.HasFlag(DragDropKeyStates.ControlKey))
                {
                    if (_model.IsSelected(pressedRow.OffsetToRoot, pressedColumn))
                    {
                        _model.Cut();
                        _model.Select(_row.OffsetToRoot, _row.OffsetToRoot, _column, _column);
                        _model.Paste();
                    }
                }
                else
                {
                    _model.Select(pressedRow.OffsetToRoot, _row.OffsetToRoot, pressedColumn, _column);
                }
            }
            else
            {
                switch (action)
                {
                    case ActionName.Empty:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name != GateName.Empty)
                        {
                            for (int i = oldGate.Begin; i <= oldGate.End; i++)
                            {
                                RegisterRefModel gateRef = _model.GetRefFromOffset(i);
                                Gate newGate = new EmptyGate(gateRef);
                                _model.Steps[_column].SetGate(newGate);
                            }

                            if (oldGate is MultiControlledGate)
                            {
                                MultiControlledGate mcg = oldGate as MultiControlledGate;
                                if (mcg.Controls.Contains<RegisterRefModel>(_row))
                                {
                                    RegisterRefModel[] toRemove = new RegisterRefModel[] { _row };
                                    RegisterRefModel[] newControls = mcg.Controls.Except<RegisterRefModel>(toRemove).ToArray();
                                    Gate toAdd = null;
                                    if (oldGate.Name == GateName.PhaseKick)
                                    {
                                        PhaseKickGate pk = oldGate as PhaseKickGate;
                                        toAdd = new PhaseKickGate(pk.Gamma, pk.Target, newControls);
                                    }
                                    else if (oldGate.Name == GateName.CPhaseShift)
                                    {
                                        CPhaseShiftGate cps = oldGate as CPhaseShiftGate;
                                        toAdd = new CPhaseShiftGate(cps.Dist, cps.Target, newControls);
                                    }
                                    else if (oldGate.Name == GateName.InvCPhaseShift)
                                    {
                                        InvCPhaseShiftGate icps = oldGate as InvCPhaseShiftGate;
                                        toAdd = new InvCPhaseShiftGate(icps.Dist, icps.Target, newControls);
                                    }
                                    else // Toffoli
                                    {
                                        if (newControls.Length > 1)
                                        {
                                            toAdd = new ToffoliGate(oldGate.Target, newControls);
                                        }
                                        else
                                        {
                                            toAdd = new CNotGate(oldGate.Target, newControls[0]);
                                        }
                                    }
                                    _model.Steps[_column].SetGate(toAdd);
                                }
                            }
                            else if (oldGate.Name == GateName.CNot)
                            {
                                if (oldGate.Control.Value.OffsetToRoot == _row.OffsetToRoot)
                                {
                                    Gate toAdd = new SigmaXGate(oldGate.Target);
                                    _model.Steps[_column].SetGate(toAdd);
                                }
                            }
                            else if (oldGate is SingleGate)
                            {
                                SingleGate sg = oldGate as SingleGate;
                                if (sg.Control.HasValue)
                                {
                                    if (sg.Control.Value.OffsetToRoot == _row.OffsetToRoot)
                                    {
                                        Gate toAdd = null;
                                        switch (sg.Name)
                                        {
                                            case GateName.Hadamard:
                                                toAdd = new HadamardGate(sg.Target);
                                                break;
                                            case GateName.PhaseScale:
                                                toAdd = new PhaseScaleGate((sg as PhaseScaleGate).Gamma, sg.Target);
                                                break;
                                            case GateName.RotateX:
                                                toAdd = new RotateXGate((sg as RotateXGate).Gamma, sg.Target);
                                                break;
                                            case GateName.RotateY:
                                                toAdd = new RotateYGate((sg as RotateYGate).Gamma, sg.Target);
                                                break;
                                            case GateName.RotateZ:
                                                toAdd = new RotateZGate((sg as RotateZGate).Gamma, sg.Target);
                                                break;
                                            case GateName.SigmaX:
                                                toAdd = new SigmaXGate(sg.Target);
                                                break;
                                            case GateName.SigmaY:
                                                toAdd = new SigmaYGate(sg.Target);
                                                break;
                                            case GateName.SigmaZ:
                                                toAdd = new SigmaZGate(sg.Target);
                                                break;
                                            case GateName.SqrtX:
                                                toAdd = new SqrtXGate(sg.Target);
                                                break;
                                            case GateName.Unitary:
                                                toAdd = new UnitaryGate((sg as UnitaryGate).Matrix, sg.Target);
                                                break;
                                            default:
                                                break;
                                        }
                                        _model.Steps[_column].SetGate(toAdd);
                                    }
                                }
                            }
                        }
                        break;
                    case ActionName.Hadamard:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new HadamardGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.SigmaX:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new SigmaXGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.SigmaY:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new SigmaYGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.SigmaZ:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new SigmaZGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.SqrtX:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            _model.Steps[_column].SetGate(new SqrtXGate(_row));
                            _model.AddStepAfter(_column);
                        }
                        break;
                    case ActionName.RotateX:
                    case ActionName.RotateY:
                    case ActionName.RotateZ:
                    case ActionName.PhaseKick:
                    case ActionName.PhaseScale:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            MainWindow window = App.Current.MainWindow as MainWindow;
                            GammaInputVM gammmaVM = new GammaInputVM();
                            ICustomContentDialog dialog = window.DialogManager.CreateCustomContentDialog(new GammaInput(gammmaVM), DialogMode.OkCancel);
                            dialog.Ok = () =>
                            {
                                double gamma = gammmaVM.Gamma;
                                if (action == ActionName.RotateX)
                                {
                                    _model.Steps[_column].SetGate(new RotateXGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                                else if (action == ActionName.RotateY)
                                {
                                    _model.Steps[_column].SetGate(new RotateYGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                                else if (action == ActionName.RotateZ)
                                {
                                    _model.Steps[_column].SetGate(new RotateZGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                                else if (action == ActionName.PhaseKick)
                                {
                                    _model.Steps[_column].SetGate(new PhaseKickGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                                else
                                {
                                    _model.Steps[_column].SetGate(new PhaseScaleGate(gamma, _row));
                                    _model.AddStepAfter(_column);
                                }
                            };
                            dialog.Show();
                        }
                        break;
                    case ActionName.Unitary:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            MainWindow window1 = App.Current.MainWindow as MainWindow;
                            MatrixInputVM matrixVM = new MatrixInputVM();
                            ICustomContentDialog dialog1 = window1.DialogManager.CreateCustomContentDialog(new MatrixInput(matrixVM), DialogMode.OkCancel);
                            dialog1.Ok = () =>
                            {
                                Complex[,] matrix = matrixVM.Matrix;
                                if (matrix != null)
                                {
                                    _model.Steps[_column].SetGate(new UnitaryGate(matrix, _row));
                                    _model.AddStepAfter(_column);
                                }
                            };
                            dialog1.Show();
                        }
                        break;
                    case ActionName.Control:
                        if (pressedColumn == _column)
                        {
                            oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                            if (!_row.Equals(pressedRow))
                            {
                                if (oldGate.Name == GateName.CNot)
                                {
                                    CNotGate oldCnot = oldGate as CNotGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldCnot.Target, oldCnot.Control.Value, pressedRow));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldCnot.Target, oldCnot.Control.Value, pressedRow));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        //check if not doubled
                                        if (_row.OffsetToRoot != oldCnot.Target.OffsetToRoot &&
                                            _row.OffsetToRoot != oldCnot.Control.Value.OffsetToRoot)
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldCnot.Target, oldCnot.Control.Value, _row));
                                        }
                                    }
                                }
                                else if (oldGate.Name == GateName.Toffoli)
                                {
                                    ToffoliGate oldT = oldGate as ToffoliGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldT.Target, pressedRow, oldT.Controls));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldT.Target, pressedRow, oldT.Controls));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        //check if not doubled
                                        if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                            !oldT.Controls.Contains<RegisterRefModel>(_row))
                                        {
                                            _model.Steps[_column].SetGate(new ToffoliGate(oldT.Target, _row, oldT.Controls));
                                        }
                                    }
                                }
                                else if (oldGate.Name == GateName.CPhaseShift)
                                {
                                    CPhaseShiftGate oldT = oldGate as CPhaseShiftGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new CPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new CPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new CPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Name == GateName.InvCPhaseShift)
                                {
                                    InvCPhaseShiftGate oldT = oldGate as InvCPhaseShiftGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new InvCPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new InvCPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new InvCPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Name == GateName.PhaseKick)
                                {
                                    PhaseKickGate oldT = oldGate as PhaseKickGate;
                                    if (pressedRow.OffsetToRoot < oldGate.Begin)
                                    {
                                        if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, oldGate.Begin - 1))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new PhaseKickGate(oldT.Gamma, oldT.Target, cParams));
                                        }
                                    }
                                    else if (pressedRow.OffsetToRoot > oldGate.End)
                                    {
                                        if (_model.Steps[_column].HasPlace(oldGate.End + 1, pressedRow.OffsetToRoot))
                                        {
                                            List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                            cList.Add(pressedRow);
                                            RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                            _model.Steps[_column].SetGate(new PhaseKickGate(oldT.Gamma, oldT.Target, cParams));
                                        }
                                    }
                                    else // new Control inside
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new PhaseKickGate(oldT.Gamma, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Control == null)
                                {
                                    int lastEmptyRow = _row.OffsetToRoot - 1;
                                    if (pressedRow.OffsetToRoot > _row.OffsetToRoot)
                                    {
                                        lastEmptyRow = lastEmptyRow + 2;
                                    }
                                    if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, lastEmptyRow))
                                    {
                                        switch (oldGate.Name)
                                        {
                                            case GateName.Hadamard:
                                                _model.Steps[_column].SetGate(new HadamardGate(_row, pressedRow));
                                                break;
                                            case GateName.PhaseScale:
                                                PhaseScaleGate oldPs = oldGate as PhaseScaleGate;
                                                _model.Steps[_column].SetGate(new PhaseScaleGate(oldPs.Gamma, _row, pressedRow));
                                                break;
                                            case GateName.RotateX:
                                                RotateXGate oldRx = oldGate as RotateXGate;
                                                _model.Steps[_column].SetGate(new RotateXGate(oldRx.Gamma, _row, pressedRow));
                                                break;
                                            case GateName.RotateY:
                                                RotateYGate oldRy = oldGate as RotateYGate;
                                                _model.Steps[_column].SetGate(new RotateYGate(oldRy.Gamma, _row, pressedRow));
                                                break;
                                            case GateName.RotateZ:
                                                RotateZGate oldRz = oldGate as RotateZGate;
                                                _model.Steps[_column].SetGate(new RotateZGate(oldRz.Gamma, _row, pressedRow));
                                                break;
                                            case GateName.SigmaX:
                                                _model.Steps[_column].SetGate(new CNotGate(_row, pressedRow));
                                                break;
                                            case GateName.SigmaY:
                                                _model.Steps[_column].SetGate(new SigmaYGate(_row, pressedRow));
                                                break;
                                            case GateName.SigmaZ:
                                                _model.Steps[_column].SetGate(new SigmaZGate(_row, pressedRow));
                                                break;
                                            case GateName.SqrtX:
                                                _model.Steps[_column].SetGate(new SqrtXGate(_row, pressedRow));
                                                break;
                                            case GateName.Unitary:
                                                UnitaryGate oldU = oldGate as UnitaryGate;
                                                _model.Steps[_column].SetGate(new UnitaryGate(oldU.Matrix, _row, pressedRow));
                                                break;
                                            case GateName.Empty:
                                                _model.Steps[_column].SetGate(new CNotGate(_row, pressedRow));
                                                _model.AddStepAfter(_column);
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                }
                            }
                            else // add control inside (only CNot or Toffoli)
                            {
                                if (oldGate.Name == GateName.CNot)
                                {
                                    CNotGate oldCnot = oldGate as CNotGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldCnot.Target.OffsetToRoot &&
                                        _row.OffsetToRoot != oldCnot.Control.Value.OffsetToRoot)
                                    {
                                        _model.Steps[_column].SetGate(new ToffoliGate(oldCnot.Target, _row, oldCnot.Control.Value));
                                    }
                                }
                                else if (oldGate.Name == GateName.Toffoli)
                                {
                                    ToffoliGate oldT = oldGate as ToffoliGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                        !oldT.Controls.Contains<RegisterRefModel>(_row))
                                    {
                                        _model.Steps[_column].SetGate(new ToffoliGate(oldT.Target, _row, oldT.Controls));
                                    }
                                }
                                else if (oldGate.Name == GateName.CPhaseShift)
                                {
                                    CPhaseShiftGate oldT = oldGate as CPhaseShiftGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                        !oldT.Controls.Contains<RegisterRefModel>(_row))
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new CPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Name == GateName.InvCPhaseShift)
                                {
                                    InvCPhaseShiftGate oldT = oldGate as InvCPhaseShiftGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                        !oldT.Controls.Contains<RegisterRefModel>(_row))
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new InvCPhaseShiftGate(oldT.Dist, oldT.Target, cParams));
                                    }
                                }
                                else if (oldGate.Name == GateName.PhaseKick)
                                {
                                    PhaseKickGate oldT = oldGate as PhaseKickGate;
                                    //check if not doubled
                                    if (_row.OffsetToRoot != oldT.Target.OffsetToRoot &&
                                        !oldT.Controls.Contains<RegisterRefModel>(_row))
                                    {
                                        List<RegisterRefModel> cList = oldT.Controls.ToList<RegisterRefModel>();
                                        cList.Add(_row);
                                        RegisterRefModel[] cParams = cList.ToArray<RegisterRefModel>();
                                        _model.Steps[_column].SetGate(new PhaseKickGate(oldT.Gamma, oldT.Target, cParams));
                                    }
                                }
                            }
                        }
                        break;
                    case ActionName.CPhaseShift:
                        if (pressedColumn == _column)
                        {
                            if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, _row.OffsetToRoot))
                            {
                                MainWindow window1 = App.Current.MainWindow as MainWindow;
                                PhaseDistInputVM vm = new PhaseDistInputVM();
                                vm.DistText = Math.Abs(pressedRow.OffsetToRoot - _row.OffsetToRoot).ToString();

                                ICustomContentDialog dialog1 = window1.DialogManager.CreateCustomContentDialog(
                                    new PhaseDistInput(vm), DialogMode.OkCancel);
                                dialog1.Ok = () =>
                                {
                                    int? dist = vm.Dist;
                                    if (dist.HasValue)
                                    {
                                        if (_row.Equals(pressedRow))
                                        {
                                            _model.Steps[_column].SetGate(new CPhaseShiftGate(dist.Value, _row));
                                        }
                                        else
                                        {
                                            _model.Steps[_column].SetGate(new CPhaseShiftGate(dist.Value, _row, pressedRow));
                                        }
                                        _model.AddStepAfter(_column);
                                    }
                                };
                                dialog1.Show();
                            }
                        }
                        break;
                    case ActionName.InvCPhaseShift:
                        if (pressedColumn == _column)
                        {
                            if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, _row.OffsetToRoot))
                            {
                                MainWindow window1 = App.Current.MainWindow as MainWindow;
                                PhaseDistInputVM vm = new PhaseDistInputVM();
                                vm.DistText = Math.Abs(pressedRow.OffsetToRoot - _row.OffsetToRoot).ToString();

                                ICustomContentDialog dialog1 = window1.DialogManager.CreateCustomContentDialog(
                                    new PhaseDistInput(vm), DialogMode.OkCancel);
                                dialog1.Ok = () =>
                                {
                                    int? dist = vm.Dist;
                                    if (dist.HasValue)
                                    {
                                        if (_row.Equals(pressedRow))
                                        {
                                            _model.Steps[_column].SetGate(new InvCPhaseShiftGate(dist.Value, _row));
                                        }
                                        else
                                        {
                                            _model.Steps[_column].SetGate(new InvCPhaseShiftGate(dist.Value, _row, pressedRow));
                                        }
                                        _model.AddStepAfter(_column);
                                    }
                                };
                                dialog1.Show();
                            }
                        }
                        break;
                    case ActionName.Measure:
                        if (pressedColumn == _column)
                        {
                            if (_model.Steps[_column].HasPlace(pressedRow.OffsetToRoot, _row.OffsetToRoot))
                            {
                                _model.Steps[_column].SetGate(new MeasureGate(pressedRow, _row));
                                _model.AddStepAfter(_column);
                            }
                        }
                        break;
                    case ActionName.Ungroup:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Parametric)
                        {
                            CircuitEvaluator eval = CircuitEvaluator.GetInstance();
                            //eval.InitFromModel(_model);

                            try
                            {
                                _model.SetStepForGates(_column);

                                int oldColumns = _model.Steps.Count;
                                int oldBegin = oldGate.Begin;
                                int oldEnd = oldGate.End;

                                eval.Decompose(oldGate as ParametricGate);
                                int columnsAdded = _model.Steps.Count - oldColumns;

                                _model.ResetStepForGates();

                                //remove old composite gate
                                for (int i = oldGate.Begin; i <= oldGate.End; i++)
                                {
                                    RegisterRefModel gateRef = _model.GetRefFromOffset(i);
                                    Gate newGate = new EmptyGate(gateRef);
                                    _model.Steps[_column].SetGate(newGate);
                                }

                                //delete step on _column (if it is empty)
                                bool isEmpty = true;
                                int j = 0;
                                while (isEmpty && j < _model.Steps[_column].Gates.Count)
                                {
                                    if (_model.Steps[_column].Gates[j].Name != GateName.Empty)
                                    {
                                        isEmpty = false;
                                    }
                                    j++;
                                }
                                if (isEmpty)
                                {
                                    _model.DeleteStep(_column);
                                    _model.Select(oldBegin, oldEnd, _column, _column + columnsAdded - 1);
                                }
                                else
                                {
                                    _model.Select(oldBegin, oldEnd, _column + 1, _column + columnsAdded);
                                }
                            }
                            catch (Exception ex)
                            {
                                string msg = "Unable to ungroup gate. Its parameters are invalid.\n" +
                                                "Inner exception:\n" +
                                                (ex.InnerException == null ? ex.Message : ex.InnerException.Message);
                                MessageBox.Show(
                                    msg,
                                    "Unable to ungroup gate",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                            }
                        }
                        else if (oldGate.Name == GateName.Composite)
                        {
                            CompositeGate cg = oldGate as CompositeGate;
                            List<Gate> toAdd = _model.GetActualGates(cg);
                            int column = _column;
                            foreach (Gate g in toAdd)
                            {
                                if (g.Name != GateName.Empty)
                                {
                                    _model.InsertStepRight(column);
                                    column++;
                                    _model.Steps[column].SetGate(g);
                                }
                            }

                            //remove old composite gate
                            for (int i = oldGate.Begin; i <= oldGate.End; i++)
                            {
                                RegisterRefModel gateRef = _model.GetRefFromOffset(i);
                                Gate newGate = new EmptyGate(gateRef);
                                _model.Steps[_column].SetGate(newGate);
                            }

                            //delete step on _column (if it is empty)
                            bool isEmpty = true;
                            int j = 0;
                            while (isEmpty && j < _model.Steps[_column].Gates.Count)
                            {
                                if (_model.Steps[_column].Gates[j].Name != GateName.Empty)
                                {
                                    isEmpty = false;
                                }
                                j++;
                            }
                            if (isEmpty)
                            {
                                _model.DeleteStep(_column);
                            }
                        }
                        break;
                    case ActionName.Composite:
                        oldGate = _model.Steps[_column].Gates[_row.OffsetToRoot];
                        if (oldGate.Name == GateName.Empty)
                        {
                            MainWindow window1 = App.Current.MainWindow as MainWindow;

                            CircuitEvaluator eval = CircuitEvaluator.GetInstance();
                            //eval.InitFromModel(_model);
                            Dictionary<string, List<MethodInfo>> dict = eval.GetExtensionGates();
                            if (!string.IsNullOrWhiteSpace(HomeVM.SelectedComposite))
                            {
                                ParametricInputVM vm = new ParametricInputVM(HomeVM.SelectedComposite, dict, _model.CompositeGates);
                                ParametricInput ci = new ParametricInput(vm);
                                ICustomContentDialog dialog1 = window1.DialogManager
                                    .CreateCustomContentDialog(ci, DialogMode.OkCancel);
                                dialog1.Ok = () =>
                                    {
                                        try
                                        {
                                            if (vm.IsValid)
                                            {
                                                if (vm.Method != null)
                                                {
                                                    ParametricGate cg = eval.CreateParametricGate(vm.Method, vm.ParamValues);
                                                    _model.Steps[_column].SetGate(cg);
                                                    _model.AddStepAfter(_column);
                                                }
                                                else if (vm.CopositeGateTarget != null)
                                                {
                                                    int minWidth = _model.MinWidthForComposite(vm.FunctionName);
                                                    if (vm.CopositeGateTarget.Value.Width < minWidth)
                                                    {
                                                        StringBuilder sb = new StringBuilder("Entered parameter has too small width.\n");
                                                        sb.Append("Entered width: ");
                                                        sb.Append(vm.CopositeGateTarget.Value.Width).AppendLine();
                                                        sb.Append("Minimum width: ");
                                                        sb.Append(minWidth).AppendLine();
                                                        throw new Exception(sb.ToString());
                                                    }
                                                    else
                                                    {
                                                        CompositeGate cg = new CompositeGate(vm.FunctionName, vm.CopositeGateTarget.Value);
                                                        _model.Steps[_column].SetGate(cg);
                                                        _model.AddStepAfter(_column);
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            string msg = "Unable to add gate. The parameters are invalid.\n" +
                                                "Inner exception:\n" +
                                                (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
                                            MessageBox.Show(
                                                 msg,
                                                "Unable to add gate",
                                                MessageBoxButton.OK,
                                                MessageBoxImage.Error);
                                        }
                                    };
                                dialog1.Show();
                            }
                        }
                        break;
                    case ActionName.Selection:
                        _model.Select(pressedRow.OffsetToRoot, _row.OffsetToRoot, pressedColumn, _column);
                        break;
                    case ActionName.Pointer:
                    default:
                        break;
                }
            }
        }
Esempio n. 2
0
        public void Delete()
        {
            if (_selectedItems.HasValue)
            {
                if (_selectedItems.Value.RowSpan == 1 && _selectedItems.Value.ColumnSpan == 1)
                {
                    int  column  = _selectedItems.Value.BeginColumn;
                    int  row     = _selectedItems.Value.BeginRow;
                    Gate oldGate = _steps[column].Gates[row];
                    if (oldGate.Name != GateName.Empty)
                    {
                        for (int i = oldGate.Begin; i <= oldGate.End; i++)
                        {
                            RegisterRefModel gateRef = GetRefFromOffset(i);
                            Gate             newGate = new EmptyGate(gateRef);
                            _steps[column].SetGate(newGate);
                        }

                        if (oldGate is MultiControlledGate)
                        {
                            MultiControlledGate mcg    = oldGate as MultiControlledGate;
                            RegisterRefModel    rowRef = GetRefFromOffset(row);
                            if (mcg.Controls.Contains <RegisterRefModel>(rowRef))
                            {
                                RegisterRefModel[] toRemove    = new RegisterRefModel[] { rowRef };
                                RegisterRefModel[] newControls = mcg.Controls.Except <RegisterRefModel>(toRemove).ToArray();
                                Gate toAdd = null;
                                if (oldGate.Name == GateName.PhaseKick)
                                {
                                    PhaseKickGate pk = oldGate as PhaseKickGate;
                                    toAdd = new PhaseKickGate(pk.Gamma, pk.Target, newControls);
                                }
                                else if (oldGate.Name == GateName.CPhaseShift)
                                {
                                    CPhaseShiftGate cps = oldGate as CPhaseShiftGate;
                                    toAdd = new CPhaseShiftGate(cps.Dist, cps.Target, newControls);
                                }
                                else if (oldGate.Name == GateName.InvCPhaseShift)
                                {
                                    InvCPhaseShiftGate icps = oldGate as InvCPhaseShiftGate;
                                    toAdd = new InvCPhaseShiftGate(icps.Dist, icps.Target, newControls);
                                }
                                else // Toffoli
                                {
                                    if (newControls.Length > 1)
                                    {
                                        toAdd = new ToffoliGate(oldGate.Target, newControls);
                                    }
                                    else
                                    {
                                        toAdd = new CNotGate(oldGate.Target, newControls[0]);
                                    }
                                }
                                _steps[column].SetGate(toAdd);
                            }
                        }
                        else if (oldGate.Name == GateName.CNot)
                        {
                            if (oldGate.Control.Value.OffsetToRoot == row)
                            {
                                Gate toAdd = new SigmaXGate(oldGate.Target);
                                _steps[column].SetGate(toAdd);
                            }
                        }
                        else if (oldGate is SingleGate)
                        {
                            SingleGate sg = oldGate as SingleGate;
                            if (sg.Control.HasValue)
                            {
                                if (sg.Control.Value.OffsetToRoot == row)
                                {
                                    Gate toAdd = null;
                                    switch (sg.Name)
                                    {
                                    case GateName.Hadamard:
                                        toAdd = new HadamardGate(sg.Target);
                                        break;

                                    case GateName.PhaseScale:
                                        toAdd = new PhaseScaleGate((sg as PhaseScaleGate).Gamma, sg.Target);
                                        break;

                                    case GateName.RotateX:
                                        toAdd = new RotateXGate((sg as RotateXGate).Gamma, sg.Target);
                                        break;

                                    case GateName.RotateY:
                                        toAdd = new RotateYGate((sg as RotateYGate).Gamma, sg.Target);
                                        break;

                                    case GateName.RotateZ:
                                        toAdd = new RotateZGate((sg as RotateZGate).Gamma, sg.Target);
                                        break;

                                    case GateName.SigmaX:
                                        toAdd = new SigmaXGate(sg.Target);
                                        break;

                                    case GateName.SigmaY:
                                        toAdd = new SigmaYGate(sg.Target);
                                        break;

                                    case GateName.SigmaZ:
                                        toAdd = new SigmaZGate(sg.Target);
                                        break;

                                    case GateName.SqrtX:
                                        toAdd = new SqrtXGate(sg.Target);
                                        break;

                                    case GateName.Unitary:
                                        toAdd = new UnitaryGate((sg as UnitaryGate).Matrix, sg.Target);
                                        break;

                                    default:
                                        break;
                                    }
                                    _steps[column].SetGate(toAdd);
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = _selectedItems.Value.BeginColumn; i <= _selectedItems.Value.EndColumn; i++)
                    {
                        for (int j = _selectedItems.Value.BeginRow; j <= _selectedItems.Value.EndRow; j++)
                        {
                            Gate oldGate = _steps[i].Gates[j];

                            for (int k = oldGate.Begin; k <= oldGate.End; k++)
                            {
                                RegisterRefModel gateRef = GetRefFromOffset(k);
                                Gate             newGate = new EmptyGate(gateRef);
                                _steps[i].SetGate(newGate);
                            }
                            j = oldGate.End;
                        }
                    }
                }
            }
        }
        public void Delete()
        {
            if (_selectedItems.HasValue)
            {
                if (_selectedItems.Value.RowSpan == 1 && _selectedItems.Value.ColumnSpan == 1)
                {
                    int column = _selectedItems.Value.BeginColumn;
                    int row = _selectedItems.Value.BeginRow;
                    Gate oldGate = _steps[column].Gates[row];
                    if (oldGate.Name != GateName.Empty)
                    {
                        for (int i = oldGate.Begin; i <= oldGate.End; i++)
                        {
                            RegisterRefModel gateRef = GetRefFromOffset(i);
                            Gate newGate = new EmptyGate(gateRef);
                            _steps[column].SetGate(newGate);
                        }

                        if (oldGate is MultiControlledGate)
                        {
                            MultiControlledGate mcg = oldGate as MultiControlledGate;
                            RegisterRefModel rowRef = GetRefFromOffset(row);
                            if (mcg.Controls.Contains<RegisterRefModel>(rowRef))
                            {
                                RegisterRefModel[] toRemove = new RegisterRefModel[] { rowRef };
                                RegisterRefModel[] newControls = mcg.Controls.Except<RegisterRefModel>(toRemove).ToArray();
                                Gate toAdd = null;
                                if (oldGate.Name == GateName.PhaseKick)
                                {
                                    PhaseKickGate pk = oldGate as PhaseKickGate;
                                    toAdd = new PhaseKickGate(pk.Gamma, pk.Target, newControls);
                                }
                                else if (oldGate.Name == GateName.CPhaseShift)
                                {
                                    CPhaseShiftGate cps = oldGate as CPhaseShiftGate;
                                    toAdd = new CPhaseShiftGate(cps.Dist, cps.Target, newControls);
                                }
                                else if (oldGate.Name == GateName.InvCPhaseShift)
                                {
                                    InvCPhaseShiftGate icps = oldGate as InvCPhaseShiftGate;
                                    toAdd = new InvCPhaseShiftGate(icps.Dist, icps.Target, newControls);
                                }
                                else // Toffoli
                                {
                                    if (newControls.Length > 1)
                                    {
                                        toAdd = new ToffoliGate(oldGate.Target, newControls);
                                    }
                                    else
                                    {
                                        toAdd = new CNotGate(oldGate.Target, newControls[0]);
                                    }
                                }
                                _steps[column].SetGate(toAdd);
                            }
                        }
                        else if (oldGate.Name == GateName.CNot)
                        {
                            if (oldGate.Control.Value.OffsetToRoot == row)
                            {
                                Gate toAdd = new SigmaXGate(oldGate.Target);
                                _steps[column].SetGate(toAdd);
                            }
                        }
                        else if (oldGate is SingleGate)
                        {
                            SingleGate sg = oldGate as SingleGate;
                            if (sg.Control.HasValue)
                            {
                                if (sg.Control.Value.OffsetToRoot == row)
                                {
                                    Gate toAdd = null;
                                    switch (sg.Name)
                                    {
                                        case GateName.Hadamard:
                                            toAdd = new HadamardGate(sg.Target);
                                            break;
                                        case GateName.PhaseScale:
                                            toAdd = new PhaseScaleGate((sg as PhaseScaleGate).Gamma, sg.Target);
                                            break;
                                        case GateName.RotateX:
                                            toAdd = new RotateXGate((sg as RotateXGate).Gamma, sg.Target);
                                            break;
                                        case GateName.RotateY:
                                            toAdd = new RotateYGate((sg as RotateYGate).Gamma, sg.Target);
                                            break;
                                        case GateName.RotateZ:
                                            toAdd = new RotateZGate((sg as RotateZGate).Gamma, sg.Target);
                                            break;
                                        case GateName.SigmaX:
                                            toAdd = new SigmaXGate(sg.Target);
                                            break;
                                        case GateName.SigmaY:
                                            toAdd = new SigmaYGate(sg.Target);
                                            break;
                                        case GateName.SigmaZ:
                                            toAdd = new SigmaZGate(sg.Target);
                                            break;
                                        case GateName.SqrtX:
                                            toAdd = new SqrtXGate(sg.Target);
                                            break;
                                        case GateName.Unitary:
                                            toAdd = new UnitaryGate((sg as UnitaryGate).Matrix, sg.Target);
                                            break;
                                        default:
                                            break;
                                    }
                                    _steps[column].SetGate(toAdd);
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = _selectedItems.Value.BeginColumn; i <= _selectedItems.Value.EndColumn; i++)
                    {
                        for (int j = _selectedItems.Value.BeginRow; j <= _selectedItems.Value.EndRow; j++)
                        {
                            Gate oldGate = _steps[i].Gates[j];

                            for (int k = oldGate.Begin; k <= oldGate.End; k++)
                            {
                                RegisterRefModel gateRef = GetRefFromOffset(k);
                                Gate newGate = new EmptyGate(gateRef);
                                _steps[i].SetGate(newGate);
                            }
                            j = oldGate.End;
                        }
                    }
                }

            }
        }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
0
        public void RotateY(double gamma, int target, int? control = null)
        {
            Validate(target, "RotateY", "target");
            if (control.HasValue)
            {
                Validate(control.Value, "RotateY", "control");
            }

            RegisterRefModel targetRef;
            RegisterRefModel? controlRef;

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