def test_qubits_with_instruction_gate(self): """Test `qubits_with_instruction`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add(U1Gate(0), (0, ), sched) inst_map.add(U1Gate(0), (1, ), sched) inst_map.add(CXGate(), [0, 1], sched) self.assertEqual(inst_map.qubits_with_instruction(U1Gate(0)), [0, 1]) self.assertEqual(inst_map.qubits_with_instruction(CXGate()), [(0, 1)]) self.assertEqual(inst_map.qubits_with_instruction('none'), [])
def test_has_from_mock_gate(self): """Test `has` and `assert_has` from mock data.""" inst_map = FakeOpenPulse2Q().defaults().instruction_schedule_map self.assertTrue(inst_map.has(U1Gate(0), [0])) self.assertTrue(inst_map.has(CXGate(), (0, 1))) self.assertTrue(inst_map.has(U3Gate(0, 0, 0), 0)) self.assertTrue(inst_map.has('measure', [0, 1])) self.assertFalse(inst_map.has(U1Gate(0), [0, 1])) with self.assertRaises(PulseError): inst_map.assert_has('dne', [0]) with self.assertRaises(PulseError): inst_map.assert_has(CXGate(), 100)
def test_qubit_instructions_gate(self): """Test `qubit_instructions`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add(U1Gate(0), (0, ), sched) inst_map.add(U1Gate(0), (1, ), sched) inst_map.add(CXGate(), [0, 1], sched) self.assertEqual(inst_map.qubit_instructions(0), ['u1']) self.assertEqual(inst_map.qubit_instructions(1), ['u1']) self.assertEqual(inst_map.qubit_instructions((0, 1)), ['cx']) self.assertEqual(inst_map.qubit_instructions(10), [])
def test_has_gate(self): """Test `has` and `assert_has`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add(U1Gate(0), (0, ), sched) inst_map.add(CXGate(), [0, 1], sched) self.assertTrue(inst_map.has(U1Gate(0), [0])) self.assertTrue(inst_map.has(CXGate(), (0, 1))) with self.assertRaises(PulseError): inst_map.assert_has('dne', [0]) with self.assertRaises(PulseError): inst_map.assert_has(CXGate(), 100)
def test_add_gate(self): """Test add, and that errors are raised when expected.""" sched = Schedule() sched.append(Play(Waveform(np.ones(5)), DriveChannel(0))) inst_map = InstructionScheduleMap() inst_map.add(U1Gate(0), 1, sched) inst_map.add(U1Gate(0), 0, sched) self.assertIn('u1', inst_map.instructions) self.assertEqual(inst_map.qubits_with_instruction(U1Gate(0)), [0, 1]) self.assertTrue('u1' in inst_map.qubit_instructions(0)) with self.assertRaises(PulseError): inst_map.add(U1Gate(0), (), sched) with self.assertRaises(PulseError): inst_map.add(U1Gate(0), 1, "not a schedule")
def test_instructions_gate(self): """Test `instructions`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add(U1Gate(0), 1, sched) inst_map.add(U3Gate(0, 0, 0), 0, sched) instructions = inst_map.instructions for inst in ['u1', 'u3']: self.assertTrue(inst in instructions)
def _circuit_u1x(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL): # Shift theta and phi so decomposition is # U1(phi).X90.U1(theta).X90.U1(lam) theta += np.pi phi += np.pi # Check for decomposition into minimimal number required X90 pulses if simplify and np.isclose(abs(theta), np.pi, atol=atol): # Zero X90 gate decomposition circuit = QuantumCircuit(1, global_phase=phase) circuit.append(U1Gate(lam + phi + theta), [0]) return circuit if simplify and np.isclose(abs(theta), np.pi / 2, atol=atol): # Single X90 gate decomposition circuit = QuantumCircuit(1, global_phase=phase) circuit.append(U1Gate(lam + theta), [0]) circuit.append(RXGate(np.pi / 2), [0]) circuit.append(U1Gate(phi + theta), [0]) return circuit # General two-X90 gate decomposition circuit = QuantumCircuit(1, global_phase=phase) circuit.append(U1Gate(lam), [0]) circuit.append(RXGate(np.pi / 2), [0]) circuit.append(U1Gate(theta), [0]) circuit.append(RXGate(np.pi / 2), [0]) circuit.append(U1Gate(phi), [0]) return circuit
def test_global_phase_u3_on_left(self): """Check proper phase accumulation with instruction with no definition.""" from qiskit.circuit.library.standard_gates import U1Gate qr = QuantumRegister(1) qc = QuantumCircuit(qr) u1 = U1Gate(0.1) u1.definition.global_phase = np.pi / 2 qc.append(u1, [0]) qc.global_phase = np.pi / 3 qc.u3(0.1, 0.2, 0.3, 0) dag = circuit_to_dag(qc) after = Optimize1qGates().run(dag) self.assertAlmostEqual(after.global_phase, 5 * np.pi / 6, 8)
def _circuit_u321(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL): qr = QuantumRegister(1, "qr") circuit = QuantumCircuit(qr, global_phase=phase) if not simplify: atol = -1.0 if abs(theta) < atol: tot = _mod_2pi(phi + lam, atol) if abs(tot) > atol: circuit._append(U1Gate(tot), [qr[0]], []) elif abs(theta - np.pi / 2) < atol: circuit._append(U2Gate(_mod_2pi(phi, atol), _mod_2pi(lam, atol)), [qr[0]], []) else: circuit._append(U3Gate(theta, _mod_2pi(phi, atol), _mod_2pi(lam, atol)), [qr[0]], []) return circuit
def test_optimize_u3_to_u1(self): """U3(0, 0, pi/4) -> U1(pi/4). Basis [u1, u2, u3].""" qr = QuantumRegister(2, 'qr') circuit = QuantumCircuit(qr) circuit.append(U3Gate(0, 0, np.pi / 4), [qr[0]]) expected = QuantumCircuit(qr) expected.append(U1Gate(np.pi / 4), [qr[0]]) basis = ['u1', 'u2', 'u3'] passmanager = PassManager() passmanager.append(BasisTranslator(sel, basis)) passmanager.append(Optimize1qGatesDecomposition(basis)) result = passmanager.run(circuit) self.assertEqual(expected, result)
def test_optimize_u3_to_u1(self): """U3(0, 0, pi/4) -> U1(pi/4). Basis [u1, u2, u3].""" qr = QuantumRegister(2, "qr") circuit = QuantumCircuit(qr) circuit.append(U3Gate(0, 0, np.pi / 4), [qr[0]]) expected = QuantumCircuit(qr) expected.append(U1Gate(np.pi / 4), [qr[0]]) basis = ["u1", "u2", "u3"] passmanager = PassManager() passmanager.append(BasisTranslator(sel, basis)) passmanager.append(Optimize1qGatesDecomposition(basis)) result = passmanager.run(circuit) msg = f"expected:\n{expected}\nresult:\n{result}" self.assertEqual(expected, result, msg=msg)
def _circuit_u321(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL): rtol = 1e-9 # default is 1e-5, too far from atol=1e-12 circuit = QuantumCircuit(1, global_phase=phase) if simplify and (np.isclose(theta, 0.0, atol=atol, rtol=rtol)): if not np.isclose( phi + lam, [0.0, 2 * np.pi], atol=atol, rtol=rtol).any(): circuit.append(U1Gate(_mod2pi(phi + lam)), [0]) elif simplify and np.isclose(theta, np.pi / 2, atol=atol, rtol=rtol): circuit.append(U2Gate(phi, lam), [0]) else: circuit.append(U3Gate(theta, phi, lam), [0]) return circuit
def _circuit_u321(theta, phi, lam, phase, simplify=True, atol=DEFAULT_ATOL): rtol = 1e-9 # default is 1e-5, too far from atol=1e-12 qr = QuantumRegister(1, 'qr') circuit = QuantumCircuit(qr, global_phase=phase) if simplify and (math.isclose(theta, 0.0, abs_tol=atol, rel_tol=rtol)): phi_lam = phi + lam if not (math.isclose(phi_lam, 0.0, abs_tol=atol, rel_tol=rtol) or math.isclose(phi_lam, 2*np.pi, abs_tol=atol, rel_tol=rtol)): circuit._append(U1Gate(_mod2pi(phi+lam)), [qr[0]], []) elif simplify and math.isclose(theta, np.pi/2, abs_tol=atol, rel_tol=rtol): circuit._append(U2Gate(phi, lam), [qr[0]], []) else: circuit._append(U3Gate(theta, phi, lam), [qr[0]], []) return circuit
def fnz(circuit, qr, phi): phi = _mod_2pi(phi, atol) if abs(phi) > atol: circuit._append(U1Gate(phi), [qr[0]], [])
from qiskit.circuit import EquivalenceLibrary, Parameter, QuantumCircuit, QuantumRegister from qiskit.circuit.library.standard_gates import U2Gate, U3Gate, HGate, U1Gate, CCXGate, TdgGate, TGate, RZGate, CXGate from qiskit.qasm import pi eq_lib = EquivalenceLibrary() theta = Parameter('theta') phi = Parameter('phi') lamda = Parameter('lamda') u3 = U3Gate(theta, phi, lamda) u2 = U2Gate(theta, phi) u1 = U1Gate(theta) ccx = CCXGate() q = QuantumRegister(1, 'q') equiv_u3 = QuantumCircuit(q) equiv_u3.append(RZGate(phi+(pi/2)), [q[0]], []) equiv_u3.append(HGate(),[q[0]], [] ) equiv_u3.append(RZGate(theta),[q[0]], []) equiv_u3.append(HGate(), [q[0]], []) equiv_u3.append(RZGate(lamda-(pi/2)),[q[0]], 0) eq_lib.add_equivalence(u3, equiv_u3) q = QuantumRegister(1, 'q') equiv_u2 = QuantumCircuit(q) equiv_u2.append(RZGate(theta+(pi/2)), [q[0]], []) equiv_u2.append(HGate(),[q[0]], [] ) equiv_u2.append(RZGate(pi/2),[q[0]], []) equiv_u2.append(HGate(), [q[0]], [])