public ulong Measure() { RegisterRefModel beginRef, endRef; if (_model == null) { beginRef = _compModel.GetRefFromOffset(0); endRef = _compModel.GetRefFromOffset(_register.Width - 1); } else { beginRef = new RegisterRefModel() { Register = _model, Offset = _offsetToModel }; endRef = new RegisterRefModel() { Register = _model, Offset = _offsetToModel + _register.Width - 1 }; } MeasureGate gate = new MeasureGate(beginRef, endRef); AddGate(gate); return(_register.Measure()); }
public bool RunStep(IList <Gate> gates, bool runBackward = false, bool runComposite = false) { Quantum.Register root = _comp.GetSourceRoot(); bool somethingChanged = false; int i = 0; int maxI = (runComposite ? 1 : gates.Count); while (i < maxI) { Gate gate = gates[i]; int? control = null; if (gate.Control.HasValue) { control = gate.Control.Value.OffsetToRoot; } switch (gate.Name) { case GateName.Hadamard: // the same as inversion root.Hadamard(gate.Target.OffsetToRoot, control); somethingChanged = true; i = gate.End + 1; break; case GateName.SigmaX: // the same as inversion root.SigmaX(gate.Target.OffsetToRoot); somethingChanged = true; i = gate.End + 1; break; case GateName.SigmaY: // the same as inversion root.SigmaY(gate.Target.OffsetToRoot, control); somethingChanged = true; i = gate.End + 1; break; case GateName.SigmaZ: // the same as inversion root.SigmaZ(gate.Target.OffsetToRoot, control); somethingChanged = true; i = gate.End + 1; break; case GateName.SqrtX: if (runBackward) { Complex[,] m = new Complex[2, 2]; Complex a = new Complex(0.5, -0.5); Complex b = new Complex(0.5, 0.5); m[0, 0] = b; m[0, 1] = a; m[1, 0] = a; m[1, 1] = b; root.Gate1(m, gate.Target.OffsetToRoot, control); } else { root.SqrtX(gate.Target.OffsetToRoot, control); } somethingChanged = true; i = gate.End + 1; break; case GateName.RotateX: RotateXGate rx = gate as RotateXGate; if (runBackward) { root.RotateX(-rx.Gamma, rx.Target.OffsetToRoot, control); } else { root.RotateX(rx.Gamma, rx.Target.OffsetToRoot, control); } somethingChanged = true; i = gate.End + 1; break; case GateName.RotateY: RotateYGate ry = gate as RotateYGate; if (runBackward) { root.RotateY(-ry.Gamma, ry.Target.OffsetToRoot, control); } else { root.RotateY(ry.Gamma, ry.Target.OffsetToRoot, control); } somethingChanged = true; i = gate.End + 1; break; case GateName.RotateZ: RotateZGate rz = gate as RotateZGate; if (runBackward) { root.RotateZ(-rz.Gamma, rz.Target.OffsetToRoot, control); } else { root.RotateZ(rz.Gamma, rz.Target.OffsetToRoot, control); } somethingChanged = true; i = gate.End + 1; break; case GateName.PhaseKick: PhaseKickGate pk = gate as PhaseKickGate; if (pk.Controls.Length > 0) { int[] controls1 = pk.Controls.Select <RegisterRefModel, int>(x => x.OffsetToRoot).ToArray <int>(); if (runBackward) { root.PhaseKick(-pk.Gamma, pk.Target.OffsetToRoot, controls1); } else { root.PhaseKick(pk.Gamma, pk.Target.OffsetToRoot, controls1); } } else { if (runBackward) { root.PhaseKick(-pk.Gamma, pk.Target.OffsetToRoot); } else { root.PhaseKick(pk.Gamma, pk.Target.OffsetToRoot); } } somethingChanged = true; i = gate.End + 1; break; case GateName.PhaseScale: PhaseScaleGate ps = gate as PhaseScaleGate; if (runBackward) { root.PhaseScale(-ps.Gamma, ps.Target.OffsetToRoot, control); } else { root.PhaseScale(ps.Gamma, ps.Target.OffsetToRoot, control); } somethingChanged = true; i = gate.End + 1; break; case GateName.CPhaseShift: CPhaseShiftGate cps = gate as CPhaseShiftGate; if (cps.Controls.Length > 0) { int[] controls1 = cps.Controls.Select <RegisterRefModel, int>(x => x.OffsetToRoot).ToArray <int>(); if (runBackward) { root.InverseCPhaseShift(cps.Dist, cps.Target.OffsetToRoot, controls1); } else { root.CPhaseShift(cps.Dist, cps.Target.OffsetToRoot, controls1); } } else { if (runBackward) { root.InverseCPhaseShift(cps.Dist, cps.Target.OffsetToRoot); } else { root.CPhaseShift(cps.Dist, cps.Target.OffsetToRoot); } } somethingChanged = true; i = gate.End + 1; break; case GateName.InvCPhaseShift: InvCPhaseShiftGate icps = gate as InvCPhaseShiftGate; if (icps.Controls.Length > 0) { int[] controls1 = icps.Controls.Select <RegisterRefModel, int>(x => x.OffsetToRoot).ToArray <int>(); if (runBackward) { root.CPhaseShift(icps.Dist, icps.Target.OffsetToRoot, controls1); } else { root.InverseCPhaseShift(icps.Dist, icps.Target.OffsetToRoot, controls1); } } else { if (runBackward) { root.CPhaseShift(icps.Dist, icps.Target.OffsetToRoot); } else { root.InverseCPhaseShift(icps.Dist, icps.Target.OffsetToRoot); } } somethingChanged = true; i = gate.End + 1; break; case GateName.CNot: CNotGate cn = gate as CNotGate; root.CNot(cn.Target.OffsetToRoot, cn.Control.Value.OffsetToRoot); somethingChanged = true; i = gate.End + 1; break; case GateName.Toffoli: ToffoliGate t = gate as ToffoliGate; int[] controls = t.Controls.Select <RegisterRefModel, int>(x => x.OffsetToRoot).ToArray <int>(); root.Toffoli(t.Target.OffsetToRoot, controls); somethingChanged = true; i = gate.End + 1; break; case GateName.Unitary: UnitaryGate u = gate as UnitaryGate; if (runBackward) { Complex[,] m = new Complex[2, 2]; m[0, 0] = Complex.Conjugate(u.Matrix[0, 0]); m[0, 1] = Complex.Conjugate(u.Matrix[1, 0]); m[1, 0] = Complex.Conjugate(u.Matrix[0, 1]); m[1, 1] = Complex.Conjugate(u.Matrix[1, 1]); root.Gate1(m, gate.Target.OffsetToRoot, control); } else { root.Gate1(u.Matrix, u.Target.OffsetToRoot, control); } somethingChanged = true; i = gate.End + 1; break; case GateName.Measure: MeasureGate mg = gate as MeasureGate; for (int j = mg.Begin; j <= mg.End; j++) { root.Measure(j); } somethingChanged = true; i = gate.End + 1; break; case GateName.Parametric: ParametricGate pg = gate as ParametricGate; _comp.Evaluate(pg, runBackward); somethingChanged = true; i = gate.End + 1; break; case GateName.Composite: CompositeGate cg = gate as CompositeGate; List <Gate> actual = _comp.GetActualGates(cg); if (runBackward) { actual.Reverse(); } foreach (Gate g in actual) { // TODO refactor RunStep() into RunGate() List <Gate> toRun = new List <Gate>() { g }; RunStep(toRun, runBackward, true); } somethingChanged = true; i = gate.End + 1; break; case GateName.Empty: default: i++; break; } } return(somethingChanged); }
internal void Delete(Register toDelete) { // before deletion, register must be measured // to remove any entanglements ulong measured = toDelete.Measure(); int offsetToRemove = toDelete.OffsetToRoot; int widthToRemove = toDelete.Width; List<Register> newChildren = new List<Register>(); for (int i = 0; i < _childRegisters.Count; i++) { Register reg = _childRegisters[i]; // all childs included in register being deleted // are also to delete if ((offsetToRemove <= reg.OffsetToRoot) && (offsetToRemove + widthToRemove >= reg.OffsetToRoot + reg.Width)) { reg.DetachRoot(); } else if (offsetToRemove + widthToRemove <= reg.OffsetToRoot) { // all registers neither included nor containing register toDelete // but their OffsetToRoot must be updated reg.OffsetToRoot -= widthToRemove; newChildren.Add(reg); } else if ((offsetToRemove >= reg.OffsetToRoot) && (offsetToRemove + widthToRemove <= reg.OffsetToRoot + reg.Width)) { // all childs containing register toDelete // their Width must be updated reg.Width -= widthToRemove; bool existTheSame = newChildren.Any<Register>(x => x.OffsetToRoot == reg.OffsetToRoot && x.Width == reg.Width); if (!existTheSame) { newChildren.Add(reg); } } else { newChildren.Add(reg); } } this._childRegisters = null; this._childRegisters = newChildren; this.Width -= toDelete.Width; ulong[] states = _amplitudes.Keys.ToArray<ulong>(); // if one of old states could be also a new state, // we must change amplitudes in order if ((measured << offsetToRemove) < ((ulong)1 << this.Width)) { Array.Sort(states); } ulong newState, leftBits, rightBits; int length = states.Length; for (int k = 0; k < length; k++) { ulong state = states[k]; rightBits = state % ((ulong)1 << offsetToRemove); leftBits = state >> (offsetToRemove + widthToRemove); newState = (leftBits << offsetToRemove) | rightBits; this._amplitudes[newState] = this._amplitudes[state]; if (newState != state) { this._amplitudes.Remove(state); } } }