def test_1_qubit_identities(self): """Tests identities for 1-qubit gates""" # T*X*T = X circ1 = QuantumCircuit(1) circ1.t(0) circ1.x(0) circ1.t(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(XGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # X*T*X = Tdg circ1 = QuantumCircuit(1) circ1.x(0) circ1.t(0) circ1.x(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(TdgGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # X*Tdg*X = T circ1 = QuantumCircuit(1) circ1.x(0) circ1.tdg(0) circ1.x(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(TGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # X*S*X = Sdg circ1 = QuantumCircuit(1) circ1.x(0) circ1.s(0) circ1.x(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(SdgGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # X*Sdg*X = S circ1 = QuantumCircuit(1) circ1.x(0) circ1.sdg(0) circ1.x(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(SGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # T*X*Tdg = S*X circ1 = QuantumCircuit(1) circ1.t(0) circ1.x(0) circ1.tdg(0) circ2 = QuantumCircuit(1) circ2.s(0) circ2.x(0) elem1 = CNOTDihedral(circ1) elem2 = CNOTDihedral(circ2) self.assertEqual(elem1, elem2, "Error: 1-qubit identity does not hold")
def test_inverse_with_different_names(self): """Test that inverse gates that have different names.""" qc = QuantumCircuit(2, 2) qc.t(0) qc.tdg(0) pass_ = InverseCancellation([(TGate(), TdgGate())]) pm = PassManager(pass_) new_circ = pm.run(qc) gates_after = new_circ.count_ops() self.assertNotIn("t", gates_after) self.assertNotIn("tdg", gates_after)
def generate_entangled(self): left_circuit = QuantumCircuit(self.width, name="q") left_dag = circuit_to_dag(left_circuit) right_circuit = QuantumCircuit(self.width, name="q") right_dag = circuit_to_dag(right_circuit) qubit_targets = {qubit: set() for qubit in range(self.width)} while True: """ Apply a random two-qubit gate to either left_dag or right_dag """ random_control_qubit_idx = self.get_random_control(qubit_targets) random_target_qubit_idx = self.get_random_target( random_control_qubit_idx, qubit_targets) dag_to_apply = random.choice([left_dag, right_dag]) random_control_qubit = dag_to_apply.qubits[ random_control_qubit_idx] random_target_qubit = dag_to_apply.qubits[random_target_qubit_idx] dag_to_apply.apply_operation_back( op=CPhaseGate(theta=0.0), qargs=[random_control_qubit, random_target_qubit], cargs=[], ) qubit_targets[random_control_qubit_idx].add( random_target_qubit_idx) """ Apply a random 1-q gate to left_dag Apply its inverse to right_dag """ single_qubit_gate = random.choice( [HGate(), TGate(), XGate(), YGate(), ZGate()]) random_qubit = left_dag.qubits[random.choice(range(self.width))] left_dag.apply_operation_back(op=single_qubit_gate, qargs=[random_qubit], cargs=[]) right_dag.apply_operation_front(op=single_qubit_gate.inverse(), qargs=[random_qubit], cargs=[]) """ Terminate when there is enough depth """ if left_dag.depth() + right_dag.depth() >= self.depth: break entangled_dag = left_dag.compose(right_dag, inplace=False) entangled_circuit = dag_to_circuit(entangled_dag) num_targets = [ len(qubit_targets[qubit]) for qubit in range(self.width) ] for qubit in range(self.width): assert num_targets[qubit] <= self.num_targets_ubs[qubit] return entangled_circuit, num_targets
def test_non_clifford_interleaved_element(self): """Verifies trying to run interleaved RB with non Clifford element throws an exception""" qubits = 1 lengths = [1, 4, 6, 9, 13, 16] interleaved_element = TGate( ) # T gate is not Clifford, this should fail self.assertRaises( QiskitError, InterleavedRB, interleaved_element, qubits, lengths, )
def random_cnotdihedral_circuit(num_qubits, num_gates, gates='all', seed=None): """Generate a pseudo random CNOTDihedral circuit.""" if gates == 'all': if num_qubits == 1: gates = ['i', 'x', 'y', 'z', 't', 'tdg', 's', 'sdg'] else: gates = [ 'i', 'x', 'y', 'z', 't', 'tdg', 's', 'sdg', 'cx', 'cz', 'swap' ] instructions = { 'i': (IGate(), 1), 'x': (XGate(), 1), 'y': (YGate(), 1), 'z': (ZGate(), 1), 's': (SGate(), 1), 'sdg': (SdgGate(), 1), 't': (TGate(), 1), 'tdg': (TdgGate(), 1), 'cx': (CXGate(), 2), 'cz': (CZGate(), 2), 'swap': (SwapGate(), 2) } if isinstance(seed, np.random.Generator): rng = seed else: rng = np.random.default_rng(seed) samples = rng.choice(gates, num_gates) circ = QuantumCircuit(num_qubits) for name in samples: gate, nqargs = instructions[name] qargs = rng.choice(range(num_qubits), nqargs, replace=False).tolist() circ.append(gate, qargs) return circ
def random_cnotdihedral_circuit(num_qubits, num_gates, gates="all", seed=None): """Generate a pseudo random CNOTDihedral circuit.""" if gates == "all": if num_qubits == 1: gates = ["i", "x", "y", "z", "t", "tdg", "s", "sdg"] else: gates = [ "i", "x", "y", "z", "t", "tdg", "s", "sdg", "cx", "cz", "swap" ] instructions = { "i": (IGate(), 1), "x": (XGate(), 1), "y": (YGate(), 1), "z": (ZGate(), 1), "s": (SGate(), 1), "sdg": (SdgGate(), 1), "t": (TGate(), 1), "tdg": (TdgGate(), 1), "cx": (CXGate(), 2), "cz": (CZGate(), 2), "swap": (SwapGate(), 2), } if isinstance(seed, np.random.Generator): rng = seed else: rng = np.random.default_rng(seed) samples = rng.choice(gates, num_gates) circ = QuantumCircuit(num_qubits) for name in samples: gate, nqargs = instructions[name] qargs = rng.choice(range(num_qubits), nqargs, replace=False).tolist() circ.append(gate, qargs) return circ
def make_immutable(obj): """ Delete the __setattr__ property to make the object mostly immutable. """ # TODO figure out how to get correct error message # def throw_immutability_exception(self, *args): # raise OpflowError('Operator convenience globals are immutable.') obj.__setattr__ = None return obj # 1-Qubit Paulis X = make_immutable(PauliOp(Pauli('X'))) Y = make_immutable(PauliOp(Pauli('Y'))) Z = make_immutable(PauliOp(Pauli('Z'))) I = make_immutable(PauliOp(Pauli('I'))) # Clifford+T, and some other common non-parameterized gates CX = make_immutable(CircuitOp(CXGate())) S = make_immutable(CircuitOp(SGate())) H = make_immutable(CircuitOp(HGate())) T = make_immutable(CircuitOp(TGate())) Swap = make_immutable(CircuitOp(SwapGate())) CZ = make_immutable(CircuitOp(CZGate())) # 1-Qubit Paulis Zero = make_immutable(StateFn('0')) One = make_immutable(StateFn('1')) Plus = make_immutable(H.compose(Zero)) Minus = make_immutable(H.compose(X).compose(Zero))
from qiskit.circuit.library import HGate, XGate, YGate, ZGate, CXGate, CYGate, CZGate, CHGate from qiskit.circuit.library import SwapGate, IGate, SGate, TGate, TdgGate, SdgGate, RYGate from qiskit import QuantumCircuit, Aer, execute, QuantumRegister, ClassicalRegister import numpy as np SINGLE_GATE_DICT = { 'I' : IGate(), 'H' : HGate(), 'X' : XGate(), 'Y' : YGate(), 'Z' : ZGate(), 'S' : SGate(), 'T' : TGate(), 'T_dg' : TdgGate(), 'S_dg' : SdgGate(), 'Ry' : RYGate(np.pi / 4) } CONTROLLED_GATE_DICT = { 'CX0' : CXGate(), 'CX1' : CXGate(), 'CY0' : CYGate(), 'CY1' : CYGate(), 'CZ0' : CZGate(), 'CZ1' : CYGate(), 'CH0' : CHGate(), 'CH1' : CHGate() } def _state_to_gates(state):
def make_immutable(obj): """ Delete the __setattr__ property to make the object mostly immutable. """ # TODO figure out how to get correct error message # def throw_immutability_exception(self, *args): # raise AquaError('Operator convenience globals are immutable.') obj.__setattr__ = None return obj # 1-Qubit Paulis X = make_immutable(PauliOp(Pauli('X'))) Y = make_immutable(PauliOp(Pauli('Y'))) Z = make_immutable(PauliOp(Pauli('Z'))) I = make_immutable(PauliOp(Pauli('I'))) # Clifford+T, and some other common non-parameterized gates CX = make_immutable(PrimitiveOp(CXGate())) S = make_immutable(PrimitiveOp(SGate())) H = make_immutable(PrimitiveOp(HGate())) T = make_immutable(PrimitiveOp(TGate())) Swap = make_immutable(PrimitiveOp(SwapGate())) CZ = make_immutable(PrimitiveOp(CZGate())) # 1-Qubit Paulis Zero = make_immutable(StateFn('0')) One = make_immutable(StateFn('1')) Plus = make_immutable(H.compose(Zero)) Minus = make_immutable(H.compose(X).compose(Zero))