def test_pairwise_entanglement_raises(self): """Test choosing pairwise entanglement raises an error for too large blocks.""" nlocal = NLocal(3, XGate(), CCXGate(), entanglement='pairwise', reps=1) # pairwise entanglement is only defined if the entangling gate has 2 qubits with self.assertRaises(ValueError): print(nlocal.draw())
def test_entanglement_by_str(self, entanglement): """Test setting the entanglement of the layers by str.""" reps = 3 nlocal = NLocal(5, rotation_blocks=XGate(), entanglement_blocks=CCXGate(), entanglement=entanglement, reps=reps) def get_expected_entangler_map(rep_num, mode): if mode == 'linear': return [(0, 1, 2), (1, 2, 3), (2, 3, 4)] elif mode == 'full': return [(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)] else: circular = [(3, 4, 0), (0, 1, 2), (1, 2, 3), (2, 3, 4)] if mode == 'circular': return circular sca = circular[-rep_num:] + circular[:-rep_num] if rep_num % 2 == 1: sca = [tuple(reversed(indices)) for indices in sca] return sca for rep_num in range(reps): entangler_map = nlocal.get_entangler_map(rep_num, 0, 3) if isinstance(entanglement, list): mode = entanglement[rep_num % len(entanglement)] else: mode = entanglement expected = get_expected_entangler_map(rep_num, mode) with self.subTest(rep_num=rep_num): # using a set here since the order does not matter self.assertEqual(set(entangler_map), set(expected))
def test_get_instruction_from_name(self): with self.assertRaises(KeyError): self.empty_target.operation_from_name("measure") self.assertEqual(self.ibm_target.operation_from_name("measure"), Measure()) self.assertEqual(self.fake_backend_target.operation_from_name("rx_30"), RXGate(math.pi / 6)) self.assertEqual( self.fake_backend_target.operation_from_name("rx"), RXGate(self.fake_backend._theta), ) self.assertEqual(self.ideal_sim_target.operation_from_name("ccx"), CCXGate())
def test_target(self): """Test target is respected by the unroll 3q or more pass.""" target = Target(num_qubits=3) target.add_instruction(CCXGate()) qc = QuantumCircuit(3) qc.ccx(0, 1, 2) qc.append(RCCXGate(), [0, 1, 2]) unroll_pass = Unroll3qOrMore(target=target) res = unroll_pass(qc) self.assertIn("ccx", res.count_ops()) self.assertNotIn("rccx", res.count_ops())
def test_entanglement_by_list(self): """Test setting the entanglement by list. This is the circuit we test (times 2, with final X layer) ┌───┐ ┌───┐┌───┐ ┌───┐ q_0: |0>┤ X ├──■────■───X────┤ X ├┤ X ├──■───X─────── .. ┤ X ├ ├───┤ │ │ │ ├───┤└─┬─┘ │ │ ├───┤ q_1: |0>┤ X ├──■────┼───┼──X─┤ X ├──■────┼───X──X──── .. ┤ X ├ ├───┤┌─┴─┐ │ │ │ ├───┤ │ │ │ x2 ├───┤ q_2: |0>┤ X ├┤ X ├──■───┼──X─┤ X ├──■────■──────X──X─ .. ┤ X ├ ├───┤└───┘┌─┴─┐ │ ├───┤ ┌─┴─┐ │ ├───┤ q_3: |0>┤ X ├─────┤ X ├─X────┤ X ├─────┤ X ├───────X─ .. ┤ X ├ └───┘ └───┘ └───┘ └───┘ └───┘ """ circuit = QuantumCircuit(4) for _ in range(2): circuit.x([0, 1, 2, 3]) circuit.barrier() circuit.ccx(0, 1, 2) circuit.ccx(0, 2, 3) circuit.swap(0, 3) circuit.swap(1, 2) circuit.barrier() circuit.x([0, 1, 2, 3]) circuit.barrier() circuit.ccx(2, 1, 0) circuit.ccx(0, 2, 3) circuit.swap(0, 1) circuit.swap(1, 2) circuit.swap(2, 3) circuit.barrier() circuit.x([0, 1, 2, 3]) layer_1_ccx = [(0, 1, 2), (0, 2, 3)] layer_1_swap = [(0, 3), (1, 2)] layer_1 = [layer_1_ccx, layer_1_swap] layer_2_ccx = [(2, 1, 0), (0, 2, 3)] layer_2_swap = [(0, 1), (1, 2), (2, 3)] layer_2 = [layer_2_ccx, layer_2_swap] entanglement = [layer_1, layer_2] nlocal = NLocal( 4, rotation_blocks=XGate(), entanglement_blocks=[CCXGate(), SwapGate()], reps=4, entanglement=entanglement, insert_barriers=True, ) self.assertCircuitEqual(nlocal, circuit)
def test_instructions(self): self.assertEqual(self.empty_target.instructions, []) ibm_expected = [ (IGate(), (0, )), (IGate(), (1, )), (IGate(), (2, )), (IGate(), (3, )), (IGate(), (4, )), (RZGate(self.theta), (0, )), (RZGate(self.theta), (1, )), (RZGate(self.theta), (2, )), (RZGate(self.theta), (3, )), (RZGate(self.theta), (4, )), (SXGate(), (0, )), (SXGate(), (1, )), (SXGate(), (2, )), (SXGate(), (3, )), (SXGate(), (4, )), (XGate(), (0, )), (XGate(), (1, )), (XGate(), (2, )), (XGate(), (3, )), (XGate(), (4, )), (CXGate(), (3, 4)), (CXGate(), (4, 3)), (CXGate(), (3, 1)), (CXGate(), (1, 3)), (CXGate(), (1, 2)), (CXGate(), (2, 1)), (CXGate(), (0, 1)), (CXGate(), (1, 0)), (Measure(), (0, )), (Measure(), (1, )), (Measure(), (2, )), (Measure(), (3, )), (Measure(), (4, )), ] self.assertEqual(ibm_expected, self.ibm_target.instructions) ideal_sim_expected = [ (UGate(self.theta, self.phi, self.lam), None), (RXGate(self.theta), None), (RYGate(self.theta), None), (RZGate(self.theta), None), (CXGate(), None), (ECRGate(), None), (CCXGate(), None), (Measure(), None), ] self.assertEqual(ideal_sim_expected, self.ideal_sim_target.instructions)
def test_compose_one_liner(self): """Test building a circuit in one line, for fun. """ circ = QuantumCircuit(3) h = HGate() rz = RZGate(0.1) cx = CXGate() ccx = CCXGate() circ = circ.compose(h, [0]).compose(cx, [0, 2]).compose(ccx, [2, 1, 0]).compose(rz, [1]) expected = QuantumCircuit(3) expected.h(0) expected.cx(0, 2) expected.ccx(2, 1, 0) expected.rz(0.1, 1) self.assertEqual(circ, expected)
def test_instruction_schedule_map_ideal_sim_backend(self): ideal_sim_target = Target(num_qubits=3) theta = Parameter("theta") phi = Parameter("phi") lam = Parameter("lambda") for inst in [ UGate(theta, phi, lam), RXGate(theta), RYGate(theta), RZGate(theta), CXGate(), ECRGate(), CCXGate(), Measure(), ]: ideal_sim_target.add_instruction(inst, {None: None}) inst_map = ideal_sim_target.instruction_schedule_map() self.assertEqual(InstructionScheduleMap(), inst_map)
def test_open_control_cxx_unrolling(self): """test unrolling of open control gates when gate is in basis""" qreg = QuantumRegister(3) qc = QuantumCircuit(qreg) ccx = CCXGate(ctrl_state=0) qc.append(ccx, [0, 1, 2]) dag = circuit_to_dag(qc) unroller = Unroller(['x', 'ccx']) unrolled_dag = unroller.run(dag) ref_circuit = QuantumCircuit(qreg) ref_circuit.x(qreg[0]) ref_circuit.x(qreg[1]) ref_circuit.ccx(qreg[0], qreg[1], qreg[2]) ref_circuit.x(qreg[0]) ref_circuit.x(qreg[1]) ref_dag = circuit_to_dag(ref_circuit) self.assertEqual(unrolled_dag, ref_dag)
def test_operations(self): self.assertEqual(self.empty_target.operations, []) ibm_expected = [ RZGate(self.theta), IGate(), SXGate(), XGate(), CXGate(), Measure() ] for gate in ibm_expected: self.assertIn(gate, self.ibm_target.operations) aqt_expected = [ RZGate(self.theta), RXGate(self.theta), RYGate(self.theta), RGate(self.theta, self.phi), RXXGate(self.theta), ] for gate in aqt_expected: self.assertIn(gate, self.aqt_target.operations) fake_expected = [ UGate(self.fake_backend._theta, self.fake_backend._phi, self.fake_backend._lam), CXGate(), Measure(), ECRGate(), RXGate(math.pi / 6), RXGate(self.fake_backend._theta), ] for gate in fake_expected: self.assertIn(gate, self.fake_backend_target.operations) ideal_sim_expected = [ UGate(self.theta, self.phi, self.lam), RXGate(self.theta), RYGate(self.theta), RZGate(self.theta), CXGate(), ECRGate(), CCXGate(), Measure(), ] for gate in ideal_sim_expected: self.assertIn(gate, self.ideal_sim_target.operations)
def test_coupling_map_3q_gate(self): fake_target = Target() ccx_props = { (0, 1, 2): None, (1, 0, 2): None, (2, 1, 0): None, } fake_target.add_instruction(CCXGate(), ccx_props) with self.assertLogs("qiskit.transpiler.target", level="WARN") as log: cmap = fake_target.build_coupling_map() self.assertEqual( log.output, [ "WARNING:qiskit.transpiler.target:" "This Target object contains multiqubit gates that " "operate on > 2 qubits. This will not be reflected in " "the output coupling map." ], ) self.assertEqual([], cmap.get_edges()) with self.assertRaises(ValueError): fake_target.build_coupling_map("ccx")
def test_skip_unentangled_qubits(self): """Test skipping the unentangled qubits.""" num_qubits = 6 entanglement_1 = [[0, 1, 3], [1, 3, 5], [0, 1, 5]] skipped_1 = [2, 4] entanglement_2 = [entanglement_1, [[0, 1, 2], [2, 3, 5]]] skipped_2 = [4] for entanglement, skipped in zip([entanglement_1, entanglement_2], [skipped_1, skipped_2]): with self.subTest(entanglement=entanglement, skipped=skipped): nlocal = NLocal(num_qubits, rotation_blocks=XGate(), entanglement_blocks=CCXGate(), entanglement=entanglement, reps=3, skip_unentangled_qubits=True) skipped_set = set(nlocal.qubits[i] for i in skipped) dag = circuit_to_dag(nlocal) idle = set(dag.idle_wires()) self.assertEqual(skipped_set, idle)
def setUp(self): super().setUp() self.fake_backend = FakeBackendV2() self.fake_backend_target = self.fake_backend.target self.theta = Parameter("theta") self.phi = Parameter("phi") self.ibm_target = Target() i_props = { (0, ): InstructionProperties(duration=35.5e-9, error=0.000413), (1, ): InstructionProperties(duration=35.5e-9, error=0.000502), (2, ): InstructionProperties(duration=35.5e-9, error=0.0004003), (3, ): InstructionProperties(duration=35.5e-9, error=0.000614), (4, ): InstructionProperties(duration=35.5e-9, error=0.006149), } self.ibm_target.add_instruction(IGate(), i_props) rz_props = { (0, ): InstructionProperties(duration=0, error=0), (1, ): InstructionProperties(duration=0, error=0), (2, ): InstructionProperties(duration=0, error=0), (3, ): InstructionProperties(duration=0, error=0), (4, ): InstructionProperties(duration=0, error=0), } self.ibm_target.add_instruction(RZGate(self.theta), rz_props) sx_props = { (0, ): InstructionProperties(duration=35.5e-9, error=0.000413), (1, ): InstructionProperties(duration=35.5e-9, error=0.000502), (2, ): InstructionProperties(duration=35.5e-9, error=0.0004003), (3, ): InstructionProperties(duration=35.5e-9, error=0.000614), (4, ): InstructionProperties(duration=35.5e-9, error=0.006149), } self.ibm_target.add_instruction(SXGate(), sx_props) x_props = { (0, ): InstructionProperties(duration=35.5e-9, error=0.000413), (1, ): InstructionProperties(duration=35.5e-9, error=0.000502), (2, ): InstructionProperties(duration=35.5e-9, error=0.0004003), (3, ): InstructionProperties(duration=35.5e-9, error=0.000614), (4, ): InstructionProperties(duration=35.5e-9, error=0.006149), } self.ibm_target.add_instruction(XGate(), x_props) cx_props = { (3, 4): InstructionProperties(duration=270.22e-9, error=0.00713), (4, 3): InstructionProperties(duration=305.77e-9, error=0.00713), (3, 1): InstructionProperties(duration=462.22e-9, error=0.00929), (1, 3): InstructionProperties(duration=497.77e-9, error=0.00929), (1, 2): InstructionProperties(duration=227.55e-9, error=0.00659), (2, 1): InstructionProperties(duration=263.11e-9, error=0.00659), (0, 1): InstructionProperties(duration=519.11e-9, error=0.01201), (1, 0): InstructionProperties(duration=554.66e-9, error=0.01201), } self.ibm_target.add_instruction(CXGate(), cx_props) measure_props = { (0, ): InstructionProperties(duration=5.813e-6, error=0.0751), (1, ): InstructionProperties(duration=5.813e-6, error=0.0225), (2, ): InstructionProperties(duration=5.813e-6, error=0.0146), (3, ): InstructionProperties(duration=5.813e-6, error=0.0215), (4, ): InstructionProperties(duration=5.813e-6, error=0.0333), } self.ibm_target.add_instruction(Measure(), measure_props) self.aqt_target = Target(description="AQT Target") rx_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(RXGate(self.theta), rx_props) ry_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(RYGate(self.theta), ry_props) rz_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(RZGate(self.theta), rz_props) r_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(RGate(self.theta, self.phi), r_props) rxx_props = { (0, 1): None, (0, 2): None, (0, 3): None, (0, 4): None, (1, 0): None, (2, 0): None, (3, 0): None, (4, 0): None, (1, 2): None, (1, 3): None, (1, 4): None, (2, 1): None, (3, 1): None, (4, 1): None, (2, 3): None, (2, 4): None, (3, 2): None, (4, 2): None, (3, 4): None, (4, 3): None, } self.aqt_target.add_instruction(RXXGate(self.theta), rxx_props) measure_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(Measure(), measure_props) self.empty_target = Target() self.ideal_sim_target = Target(num_qubits=3, description="Ideal Simulator") self.lam = Parameter("lam") for inst in [ UGate(self.theta, self.phi, self.lam), RXGate(self.theta), RYGate(self.theta), RZGate(self.theta), CXGate(), ECRGate(), CCXGate(), Measure(), ]: self.ideal_sim_target.add_instruction(inst, {None: None})
def test_controlled_cx(self): """Test creation of controlled cx gate""" self.assertEqual(CXGate().control(), CCXGate())
def test_simple_gates(self): """Check simple commutation relations between gates, experimenting with different orders of gates, different orders of qubits, different sets of qubits over which gates are defined, and so on.""" comm_checker = CommutationChecker() # should commute res = comm_checker.commute(ZGate(), [0], [], CXGate(), [0, 1], []) self.assertTrue(res) # should not commute res = comm_checker.commute(ZGate(), [1], [], CXGate(), [0, 1], []) self.assertFalse(res) # should not commute res = comm_checker.commute(XGate(), [0], [], CXGate(), [0, 1], []) self.assertFalse(res) # should commute res = comm_checker.commute(XGate(), [1], [], CXGate(), [0, 1], []) self.assertTrue(res) # should not commute res = comm_checker.commute(XGate(), [1], [], CXGate(), [1, 0], []) self.assertFalse(res) # should commute res = comm_checker.commute(XGate(), [0], [], CXGate(), [1, 0], []) self.assertTrue(res) # should commute res = comm_checker.commute(CXGate(), [1, 0], [], XGate(), [0], []) self.assertTrue(res) # should not commute res = comm_checker.commute(CXGate(), [1, 0], [], XGate(), [1], []) self.assertFalse(res) # should commute res = comm_checker.commute( CXGate(), [1, 0], [], CXGate(), [1, 0], [], ) self.assertTrue(res) # should not commute res = comm_checker.commute( CXGate(), [1, 0], [], CXGate(), [0, 1], [], ) self.assertFalse(res) # should commute res = comm_checker.commute( CXGate(), [1, 0], [], CXGate(), [1, 2], [], ) self.assertTrue(res) # should not commute res = comm_checker.commute( CXGate(), [1, 0], [], CXGate(), [2, 1], [], ) self.assertFalse(res) # should commute res = comm_checker.commute( CXGate(), [1, 0], [], CXGate(), [2, 3], [], ) self.assertTrue(res) res = comm_checker.commute(XGate(), [2], [], CCXGate(), [0, 1, 2], []) self.assertTrue(res) res = comm_checker.commute(CCXGate(), [0, 1, 2], [], CCXGate(), [0, 2, 1], []) self.assertFalse(res)