def from_label(cls, label): """Return a tensor product of single-qubit operators. Args: label (string): single-qubit operator string. Returns: Operator: The N-qubit operator. Raises: QiskitError: if the label contains invalid characters, or the length of the label is larger than an explicitly specified num_qubits. Additional Information: The labels correspond to the single-qubit matrices: 'I': [[1, 0], [0, 1]] 'X': [[0, 1], [1, 0]] 'Y': [[0, -1j], [1j, 0]] 'Z': [[1, 0], [0, -1]] 'H': [[1, 1], [1, -1]] / sqrt(2) 'S': [[1, 0], [0 , 1j]] 'T': [[1, 0], [0, (1+1j) / sqrt(2)]] '0': [[1, 0], [0, 0]] '1': [[0, 0], [0, 1]] '+': [[0.5, 0.5], [0.5 , 0.5]] '-': [[0.5, -0.5], [-0.5 , 0.5]] 'r': [[0.5, -0.5j], [0.5j , 0.5]] 'l': [[0.5, 0.5j], [-0.5j , 0.5]] """ # Check label is valid label_mats = { 'I': IGate().to_matrix(), 'X': XGate().to_matrix(), 'Y': YGate().to_matrix(), 'Z': ZGate().to_matrix(), 'H': HGate().to_matrix(), 'S': SGate().to_matrix(), 'T': TGate().to_matrix(), '0': np.array([[1, 0], [0, 0]], dtype=complex), '1': np.array([[0, 0], [0, 1]], dtype=complex), '+': np.array([[0.5, 0.5], [0.5, 0.5]], dtype=complex), '-': np.array([[0.5, -0.5], [-0.5, 0.5]], dtype=complex), 'r': np.array([[0.5, -0.5j], [0.5j, 0.5]], dtype=complex), 'l': np.array([[0.5, 0.5j], [-0.5j, 0.5]], dtype=complex), } if re.match(r'^[IXYZHST01rl\-+]+$', label) is None: raise QiskitError('Label contains invalid characters.') # Initialize an identity matrix and apply each gate num_qubits = len(label) op = Operator(np.eye(2**num_qubits, dtype=complex)) for qubit, char in enumerate(reversed(label)): if char != 'I': op = op.compose(label_mats[char], qargs=[qubit]) return op
def test_from_label(self): """Test from_label method""" label = 'IXYZHS' CI = Clifford(IGate()) CX = Clifford(XGate()) CY = Clifford(YGate()) CZ = Clifford(ZGate()) CH = Clifford(HGate()) CS = Clifford(SGate()) target = CI.tensor(CX).tensor(CY).tensor(CZ).tensor(CH).tensor(CS) self.assertEqual(Clifford.from_label(label), target)
def make_oneq_cliffords(): """Make as list of 1q Cliffords""" ixyz_list = [g().to_matrix() for g in (IGate, XGate, YGate, ZGate)] ih_list = [g().to_matrix() for g in (IGate, HGate)] irs_list = [IGate().to_matrix(), SdgGate().to_matrix() @ HGate().to_matrix(), HGate().to_matrix() @ SGate().to_matrix()] oneq_cliffords = [Operator(ixyz @ ih @ irs) for ixyz in ixyz_list for ih in ih_list for irs in irs_list] return oneq_cliffords
def to_instruction(self): """Convert to Pauli circuit instruction.""" from qiskit.circuit import QuantumCircuit, QuantumRegister from qiskit.extensions.standard import IGate, XGate, YGate, ZGate gates = {'I': IGate(), 'X': XGate(), 'Y': YGate(), 'Z': ZGate()} label = self.to_label() n_qubits = self.numberofqubits qreg = QuantumRegister(n_qubits) circuit = QuantumCircuit(qreg, name='Pauli:{}'.format(label)) for i, pauli in enumerate(reversed(label)): circuit.append(gates[pauli], [qreg[i]]) return circuit.to_instruction()
def random_clifford_circuit(num_qubits, num_gates, gates='all', seed=None): """Generate a pseudo random Clifford circuit.""" if gates == 'all': if num_qubits == 1: gates = ['i', 'x', 'y', 'z', 'h', 's', 'sdg', 'v', 'w'] else: gates = [ 'i', 'x', 'y', 'z', 'h', 's', 'sdg', 'v', 'w', 'cx', 'cz', 'swap' ] instructions = { 'i': (IGate(), 1), 'x': (XGate(), 1), 'y': (YGate(), 1), 'z': (ZGate(), 1), 'h': (HGate(), 1), 's': (SGate(), 1), 'sdg': (SdgGate(), 1), 'v': (VGate(), 1), 'w': (WGate(), 1), 'cx': (CXGate(), 2), 'cz': (CZGate(), 2), 'swap': (SwapGate(), 2) } if isinstance(seed, np.random.RandomState): rng = seed else: rng = np.random.RandomState(seed=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