def test_rz(): assert _allclose(xp.dot(rz(0.1), xp.conj(rz(0.1).T)), xp.eye(2)) _rz = lambda phi: cmath.exp(0.5j * phi) * rz(phi) assert _allclose(_rz(np.pi * 2), I) assert _allclose(_rz(np.pi), Z) assert _allclose(_rz(np.pi / 2), S) assert _allclose(_rz(np.pi / 4), T) assert _allclose(_rz(-np.pi / 2), Sdag) assert _allclose(_rz(-np.pi / 4), Tdag)
def test_phase_shift(): assert _allclose(xp.dot(phase_shift(0.1), xp.conj(phase_shift(0.1).T)), xp.eye(2)) _rz = lambda phi: cmath.exp(0.5j * phi) * rz(phi) assert _allclose(phase_shift(0.1), _rz(0.1))
def test_ry(): assert _allclose(xp.dot(ry(0.1), xp.conj(ry(0.1).T)), xp.eye(2)) _ry = lambda phi: cmath.exp(0.5j * phi) * ry(phi) assert _allclose(_ry(np.pi * 2), I) assert _allclose(_ry(np.pi), Y) assert _allclose(ry(0.1), dot(S, H, rz(0.1), H, Sdag))
def test_rx(): assert _allclose(xp.dot(rx(0.1), xp.conj(rx(0.1).T)), xp.eye(2)) _rx = lambda phi: cmath.exp(0.5j * phi) * rx(phi) assert _allclose(_rx(np.pi * 2), I) assert _allclose(_rx(np.pi), X) assert _allclose(rx(0.1), dot(H, rz(0.1), H))
def test_gate_single_qubit(): q = Qubits(1) q.gate(Y, target=0) assert _allclose(q.state, xp.array([0, 1j])) q = Qubits(1) q.gate(ry(0.1), target=0) q.gate(rz(0.1), target=0) psi1 = q.state q = Qubits(1) q.gate(xp.dot(rz(0.1), ry(0.1)), target=0) psi2 = q.state q = Qubits(1) q.gate(xp.dot(ry(0.1), rz(0.1)), target=0) psi3 = q.state assert _allclose(psi1, psi2) assert not _allclose(psi1, psi3)
def test_gate_single_qubit(): q = Qubits(1) q.gate(Y, target=0) assert np.allclose( q.data, np.array([0, 1j]) ) q = Qubits(1) q.gate(ry(0.1), target=0) q.gate(rz(0.1), target=0) psi1 = q.data q = Qubits(1) q.gate(np.dot(rz(0.1), ry(0.1)), target=0) psi2 = q.data q = Qubits(1) q.gate(np.dot(ry(0.1), rz(0.1)), target=0) psi3 = q.data assert np.allclose(psi1, psi2) assert not np.allclose(psi1, psi3)
def expm_pauli(q, theta, op): """expm_pauli(q, theta, op) applies Pauli rotation $expm(-i*theta*op)$ on the Qubits instance q Args: q (:class:`qupy.qubit.Qubits`): the state which you want to apply theta (:class:`float`): rotation angle op (:class:`str`) the pauli string """ target_list = [i for i, p in enumerate(op) if p != "I"] cnot_target = target_list[-1] if op[cnot_target] == "X": q.gate(operator.H, target=cnot_target) elif op[cnot_target] == "Y": q.gate(operator.S, target=cnot_target) q.gate(operator.H, target=cnot_target) for target in target_list[:-1]: targ = int(target) if op[targ] == "X": #print("H {}".format(i)) q.gate(operator.H, target=targ) elif op[targ] == "Y": #print("HS {}".format(i)) q.gate(operator.S, target=targ) q.gate(operator.H, target=targ) #print("CNOT({}, {})".format(i, target)) q.gate(operator.X, control=targ, target=cnot_target) q.gate(operator.rz(2 * theta), target=cnot_target) for target in target_list[:-1]: targ = int(target) q.gate(operator.X, control=targ, target=cnot_target) if op[targ] == "X": q.gate(operator.H, target=targ) elif op[targ] == "Y": q.gate(operator.H, target=targ) q.gate(operator.Sdag, target=targ) if op[cnot_target] == "X": q.gate(operator.H, target=cnot_target) elif op[cnot_target] == "Y": q.gate(operator.H, target=cnot_target) q.gate(operator.Sdag, target=cnot_target)
def circuitFromLine(self, line): self.gateNo += 1 line = line.split('#')[0].strip() if (line.startswith('qubits')): qubits = int(line[7:]) if (self.is_nqs()): self.nqs = nq.nqs.NQS(qubits, self.numInitialHidden, self.numSampleSteps, self.numRandomRestarts, self.earlyStopping, self.optimizer) else: self.exact = Qubits(qubits) for q in range(qubits): self.exact.gate(H, target=q) elif (line.startswith('X')): for q in self.readQubits(line.strip('X')): if (self.is_nqs()): self.nqs.applyPauliX(q) else: self.exact.gate(X, target=q) elif (line.startswith('Y')): for q in self.readQubits(line.strip('Y')): if (self.is_nqs()): self.nqs.applyPauliY(q) else: self.exact.gate(Y, target=q) elif (line.startswith('Z')): for q in self.readQubits(line.strip('Z')): if (self.is_nqs()): self.nqs.applyPauliZ(q) else: self.exact.gate(Z, target=q) elif (line.startswith('H')): for q in self.readQubits(line.strip('H')): if (self.is_nqs()): self.nqs.learnHadamard(q, self.numSamples, self.numIterations) else: self.exact.gate(H, target=q) elif (line.startswith('Rz')): for q in self.readQubits(line.split(',')[0].strip('Rz')): if (self.is_nqs()): self.nqs.applySingleZRotation(q, float(line.split(',')[1])) else: self.exact.gate(rz(float(line.split(',')[1])), target=q) elif (line.startswith('Toffoli')): q0 = self.readQubits(line.strip('Toffoli').split(',')[0])[0] q1 = self.readQubits(line.strip('Toffoli').split(',')[1])[0] q2 = self.readQubits(line.strip('Toffoli').split(',')[2])[0] if (self.is_nqs()): self.nqs.applyToffoli(q0, q1, q2, self.numSamples, self.numIterations) else: #https://arxiv.org/pdf/0803.2316.pdf self.exact.gate(H, target=q2) self.exact.gate(X, target=q2, control=q1) self.exact.gate(Tdag, target=q2) self.exact.gate(X, target=q2, control=q0) self.exact.gate(T, target=q2) self.exact.gate(X, target=q2, control=q1) self.exact.gate(Tdag, target=q2) self.exact.gate(X, target=q2, control=q0) self.exact.gate(T, target=q1) self.exact.gate(T, target=q2) self.exact.gate(H, target=q2) self.exact.gate(X, target=q1, control=q0) self.exact.gate(T, target=q0) self.exact.gate(Tdag, target=q1) self.exact.gate(X, target=q1, control=q0) elif (line.startswith('Tdag')): for q in self.readQubits(line.strip('Tdag')): if (self.is_nqs()): self.nqs.applyTDagger(q) else: self.exact.gate(T, target=q) elif (line.startswith('T')): for q in self.readQubits(line.strip('T')): if (self.is_nqs()): self.nqs.applyT(q) else: self.exact.gate(T, target=q) elif (line.startswith('sqrt_X')): for q in self.readQubits(line.strip('sqrt_X')): if (self.is_nqs()): self.nqs.learnSqrtX(q, self.numSamples, self.numIterations) else: self.exact.gate(sqrt_X, target=q) elif (line.startswith('sqrt_Y')): for q in self.readQubits(line.strip('sqrt_Y')): if (self.is_nqs()): self.nqs.learnSqrtY(q, self.numSamples, self.numIterations) else: self.exact.gate(sqrt_Y, target=q) elif (line.startswith('CZ')): q0 = self.readQubits(line.strip('CZ').split(',')[0])[0] q1 = self.readQubits(line.strip('CZ').split(',')[1])[0] if (self.is_nqs()): if (self.learnCZ): self.nqs.learnControlledZRotation(q0, q1, cmath.pi, self.numSamples, self.numIterations) else: self.nqs.applyControlledZRotation(q0, q1, cmath.pi) else: self.exact.gate(Z, target=q0, control=q1) else: # no CNOT, named and repeated subcircuits among others... self.gateNo -= 1
def rz(self, phase, qubit_name): self.q.gate(rz(phase), target=self.qubit_name_dict[qubit_name])
print(q.data.flatten()) q.gate(H, target=0) q.gate(H, target=1) print(q.data.flatten()) q.data = [0, 1, 0, 0, 0, 0, 0, 0] q.gate(X, target=2) print(q.data.flatten()) q.gate(H, target=0) q.gate(H, target=1) q.gate(X, target=2, control=(0, 1)) q.gate(X, target=0, control=1, control_0=2) q.gate(swap, target=(0, 2)) q.gate(rz(np.pi / 8), target=2, control_0=1) print(q.data.flatten()) q.gate(iswap, target=(2, 1)) print(q.data.flatten()) res = q.projection(target=1) print(res) from hamiltonian import Hamiltonian ham = Hamiltonian(3, coefs=[2, 1, 1], ops=["XII", "IYI", "IIZ"]) q.set_state("000") q.gate(H, target=0) q.gate(H, target=1) q.gate(S, target=1) print(q.get_state())